Page 110 - Beginning PHP 5.3
P. 110

Part II: Learning the Language
                   This code displays the first 10 numbers in the Fibonacci sequence. First the XHTML page header and
                 table header are displayed. Then a   for  loop generates each Fibonacci number, breaking out into HTML
                each time through the loop to display a table row containing the number. Notice how the script flips
                between HTML markup and PHP code several times using the    < ?php ... ? >   tags. The alternating
                table rows are achieved with a CSS class in the   head  element combined with an  if  decision embedded
                within the table row markup.

                  You can see how easy it is to output entire chunks of HTML  —  in this case, a table row  —  from inside a
                loop, or as the result of a decision.


                  Summary
                   In this chapter you explored two key concepts of PHP (or any programming language for that matter):
                 decisions and loops. Decisions let you choose to run a block of code based on the value of an expression,
                 and include:

                   ❑     The   if  statement for making simple  “ either/or ”  decisions
                   ❑     The   else  and  elseif  statements for decisions with multiple outcomes
                   ❑     The   switch  statement for running blocks of code based on the value of an expression
                   ❑     The   ?  (ternary) operator for writing compact  if...else  style decisions

                   Loops allow you to run the same block of code many times until a certain condition is met. You
                 learned about:

                   ❑       while  loops that test the condition at the start of the loop
                   ❑       do...while  loops that test the condition at the end of the loop

                   ❑       for  loops that let you write neat  “ counting ”  loops
                   You also looked at other loop - related statements, including the   break  statement for exiting a loop and the
                   continue  statement for skipping the current loop iteration. Finally, you explored nested loops, and looked
                at a powerful feature of PHP: the ability to mix decision and looping statements with HTML markup.

                In the next chapter you take a thorough look at strings in PHP, and how to manipulate them. Before
                reading it, though, try the following two exercises to cement your understanding of decisions and loops.
                As always, you can find solutions to the exercises in Appendix A.


                  Exercises

                      1.       Write a script that counts from 1 to 10 in steps of 1. For each number, display whether that
                       number is an odd or even number, and also display a message if the number is a prime number.
                       Display this information within an HTML table.
                      2.       Modify the homing pigeon simulator to simulate two different pigeons on the same map, both
                       flying to the same home point. The simulation ends when both pigeons have arrived home.





              72





                                                                                                      9/21/09   8:52:14 AM
          c04.indd   72
          c04.indd   72                                                                               9/21/09   8:52:14 AM
   105   106   107   108   109   110   111   112   113   114   115