Page 97 - Beginning PHP 5.3
P. 97

Chapter 4: Decisions and Loops
                           Doing Repetitive Tasks with Looping

                           You can see that the ability to make decisions  —  running different blocks of code based on certain
                         criteria  —  can add a lot of power to your PHP scripts. Looping can make your scripts even more
                         powerful and useful.

                           The basic idea of a loop is to run the same block of code again and again, until a certain condition is met.
                         As with decisions, that condition must take the form of an expression. If the expression evaluates to
                           true , the loop continues running. If the expression evaluates to  false , the loop exits, and execution
                         continues on the first line following the loop ’ s code block.
                           You look at three main types of loops in this chapter:

                            ❑       while  loops
                            ❑       do...while  loops
                            ❑       for  loops

                               You explore   foreach()  loops, which work specifically with arrays, in Chapter  6 .

                           Simple Looping with the while Statement

                           The simplest type of loop to understand uses the  while  statement. A  while  construct looks very similar
                          to an   if  construct:
                             while (   expression  ) {
                               // Run this code
                             }
                             // More code here

                          Here ’ s how it works. The expression inside the parentheses is tested; if it evaluates to  true , the code
                          block inside the braces is run. Then the expression is tested again; if it ’ s still   true , the code block is run
                          again, and so on. However, if at any point the expression is   false , the loop exits and execution
                          continues with the line after the closing brace.

                           Here ’ s a simple, practical example of a   while  loop:
                               < ?php

                             $widgetsLeft = 10;
                             while ( $widgetsLeft  >  0 ) {
                               echo “Selling a widget... “;

                                             ;
                               $widgetsLeft - -
                               echo “done. There are $widgetsLeft widgets left. < br / > ”;
                             }
                             echo “We ’ re right out of widgets!”;

                             ? >

                                                                                                          59





                                                                                                      9/21/09   8:52:09 AM
          c04.indd   59
          c04.indd   59                                                                               9/21/09   8:52:09 AM
   92   93   94   95   96   97   98   99   100   101   102