Page 105 - Beginning PHP 5.3
P. 105

Chapter 4: Decisions and Loops
                               elseif ( $pigeonY > $homeY )
                                 $pigeonY--;
                               // Display the current map

                               echo ‘<div class=”map” style=”width: ‘ . $mapSize . ‘em;”><pre>’;
                               for ( $y = 0; $y < $mapSize; $y++ ) {

                                 for ( $x = 0; $x < $mapSize; $x++ ) {
                                   if ( $x == $homeX && $y == $homeY ) {
                                     echo ‘<span class=”home”>+</span>’; // Home
                                   } elseif ( $x == $pigeonX && $y == $pigeonY ) {
                                     echo ‘<span class=”pigeon”>%</span>’; // Pigeon
                                   } else {
                                     echo ‘<span class=”empty”>.</span>’; // Empty square
                                   }
                                   echo ( $x != $mapSize - 1 ) ? “ “ : “”;
                                 }

                                 echo “\n”;
                               }

                               echo “</pre></div>\n”;
                             } while ( $pigeonX != $homeX || $pigeonY != $homeY );

                             ?>
                               </body>
                             </html>

                         To try out the script, save it as homing_pigeon.php in your document root folder, and open the script’s
                          URL in your Web browser. You should see something like Figure 4-4. Each map represents the progress
                          of the pigeon (represented by the % symbol) toward its home (the + symbol). Reload the page to run a
                         new simulation, with the home and the pigeon in different positions.

                             If your page looks different, make sure your document root folder contains the common.css file
                             described in Chapter 2.


















                                                                                                          67





                                                                                                      9/21/09   8:52:12 AM
          c04.indd   67
          c04.indd   67                                                                               9/21/09   8:52:12 AM
   100   101   102   103   104   105   106   107   108   109   110