Searching...
Saturday 22 June 2013

PHP Basics – Part 8 :: Control Structures - Part 2


Looping Statements

Although varied approaches exist, looping statements are a fixture in every widespread programming language. Looping mechanisms offer a simple means for accomplishing a commonplace task in programming: repeating a sequence of instructions until a specific condition is satisfied. PHP offers several such mechanisms, none of which should come as a surprise if you’re familiar with other programming languages. Don't worry if you are not familiar. It's easy.

The while Statement

The while statement specifies a condition that must be met before execution of its embedded code(Embedding PHP Code in Your Web Pages) is terminated. Its syntax is the following:

while (expression) {
statements
}

In the following example, $count is initially assigned the value 1. The value of $count is then squared and output. The $count variable(more about constants and variables) is then incremented by 1, and the loop is repeated until the value of $count reaches 5.

<?php
$count = 1;
while ($count < 5) {
printf("%d squared = %d <br />", $count, pow($count, 2));
$count++;
}
?>

The output looks like this:

1 squared = 1
2 squared = 4
3 squared = 9
4 squared = 16

Like all other control structures, multiple conditional expressions may also be embedded into the while statement. For instance, the following while block evaluates either until it reaches the end-of-file or until five lines have been read and output:

<?php
$linecount = 1;
$fh = fopen("sports.txt","r");
while (!feof($fh) && $linecount<=5)
{
$line = fgets($fh, 4096);
echo $line. "<br />";
$linecount++;
}
?>

Given these conditionals, a maximum of five lines will be output from the sports.txt file, regardless of its size.

The do...while Statement

The do...while looping statement is a variant of while but it verifies the loop conditional at the conclusion of the block rather than at the beginning. The following is its syntax:

do {
statements
} while (expression);

Both while and do...while are similar in function. The only real difference is that the code embedded within a while statement possibly could never be executed, whereas the code embedded within a do...while statement will always execute at least once. Consider the following example:

<?php
$count = 11;
do {
printf("%d squared = %d <br />", $count, pow($count, 2));
} while ($count < 10);
?>

The following is the outcome:

11 squared = 121

Despite the fact that 11 is out of bounds of the while conditional, the embedded code will execute once because the conditional is not evaluated until the conclusion. There would have no output in while statement.

The for Statement

The for statement offers a somewhat more complex looping mechanism than does while... Don't worry it's not that complex. The following is its syntax:

for (expression1; expression2; expression3) {
statements
}

There are a few rules to keep in mind when using PHP’s for loops:
  • The first expression- expression1, is evaluated by default at the first iteration of the loop.
  • The second expression- expression2, is evaluated at the beginning of each iteration. This expression determines whether looping will continue.
  • The third expression- expression3, is evaluated at the conclusion of each loop.
  • Any of the expressions can be empty, their purpose substituted by logic embedded within the for block.
Take this example for instance which display a partial kilometer/mile equivalency chart-

for ($kilometers = 1; $kilometers <= 5; $kilometers++)
{
printf("%d kilometers = %f miles <br />", $kilometers, $kilometers*0.62140);
}

$kilometers = 1 specifies the initial value of $kilometers
$kilometers <= 5 specifies the condition for the loop to continue. It'll be checked every time the loop is executed.
$kilometers++ specifies that at the end of each iteration $kilometers would be incremented by 1 and the loop will continue.

Output:

1 kilometers = 0.6214 miles
2 kilometers = 1.2428 miles
3 kilometers = 1.8642 miles
4 kilometers = 2.4856 miles
5 kilometers = 3.107 miles

Consider the following examples, all of which display a partial kilometer/mile equivalency chart:

// Example One
for ($kilometers = 1; $kilometers <= 5; $kilometers++)
{
printf("%d kilometers = %f miles <br />", $kilometers, $kilometers*0.62140);
}

// Example Two
for ($kilometers = 1; ; $kilometers++)
{
if ($kilometers > 5) break;
printf("%d kilometers = %f miles <br />", $kilometers, $kilometers*0.62140);
}

// Example Three
$kilometers = 1;
for (;;)
{
// if $kilometers > 5 break out of the for loop.
if ($kilometers > 5) break;
printf("%d kilometers = %f miles <br />", $kilometers, $kilometers*0.62140);
$kilometers++;
}

The results for all three examples follow:

1 kilometers = 0.6214 miles
2 kilometers = 1.2428 miles
3 kilometers = 1.8642 miles
4 kilometers = 2.4856 miles
5 kilometers = 3.107 miles


0 comments:

Post a Comment

 
Back to top!