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

(singke) #1

Most for loops look like Listing 3.9. They use a counter that increments by one each
time through the loop. However, the for statement is not particular about what you put in
the three slots. You can use more complex expressions if you wish. The initialization slot
allows a comma-separated list of assignments. This can be used to assign values to two or
more variables. You may also leave a slot blank. Listing 3.10 converts the code in Listing
3.6 into a for loop. I've added line breaks to the for statement to keep the code from
wrapping. It also makes it easier to see the three parts. Although the for statement is
longer and looks more complicated, it really is no different from the simple example in
Listing 3.9. A variable, in this case currentDate, is set to some initial value. That value
is used to test for an end condition. And the value is incremented by the number of
seconds in a day instead of just one.


Listing 3.10 Using for to Print Day Names

Free download pdf