Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

The do...while Statement


You can delay the decision to continue executing a loop until the end by using a
do...while statement. Listing 3.8 retools Listing 3.7. You won't notice a difference
unless you run the script on a Friday. On Fridays the original will print nothing in its list
of days. The new version will put Friday in the list because the body of the loop is
executed before currentDate is tested. By switching to a do...while loop, the loop
now lists the days until next Friday.


Listing 3.8 Using do...while to Print Day Names


<?
/*
get the current date in number of seconds
/
$currentDate = time();
/

print some text explaining the output
/
print("Days left before next Friday:\n");
print("

    \n");
    do
    {
    /

    print day name
    /
    print("
  1. ". date("l", $currentDate). "\n");
    /

    add 24 hours to currentDate
    /
    $currentDate += (60
    60 * 24);
    }
    while(date("l", $currentDate) != "Friday");
    print("
\n");
?>


The for Statement


Strictly speaking, the for loop is unnecessary. Any for loop can be implemented as
easily as a while loop. What for offers is not new functionality, but a better structure for
building the most common loops. Many loops involve incrementing a counter variable
every time through the loop, iterating until some maximum is reached.


Imagine that you wanted to step through the numbers 1 through 10. Using while, you
would first set a variable to be 1. Then you would make a while loop that tests if your
counter is less than or equal to 10. Inside the code block you would increment your
counter, making sure you do this as the last statement in the block.

Free download pdf