PHP
  Home arrow PHP arrow Page 4 - PHP 101 (part 3) - Chocolate Fudge And Time Machines
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

PHP 101 (part 3) - Chocolate Fudge And Time Machines
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2000-08-15


    Table of Contents:
  • PHP 101 (part 3) - Chocolate Fudge And Time Machines
  • Back To The Future
  • Revisiting The Past
  • Doing It By The Numbers
  • Anyone For Apple Pie?
  • The Generation Gap
  • What's That Noise?
  • Checking The Boxes
  • Miscellaneous Notes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    PHP 101 (part 3) - Chocolate Fudge And Time Machines - Doing It By The Numbers
    ( Page 4 of 9 )

    Both the "while" and "do-while" loops continue to iterate for so long as the specified conditional expression remains true. But there often arises a need to execute a certain set of statements a specific number of times - for example, printing a series of thirteen sequential numbers, or repeating a particular set of <TD>> cells five times. In such cases, clever programmers reach for the "for" loop...

    The "for" loop typically looks like this:


    for (initial value of counter; condition; update counter) { do this! }

    Looks like gibberish? Well, hang in there for a minute...the "counter" here is a PHP variable that is initialized to a numeric value, and keeps track of the number of times the loop is executed. Before each execution of the loop, the "condition" is tested - if it evaluates to true, the loop will execute once more and the counter will be appropriately incremented; if it evaluates to false, the loop will be broken and the lines following it will be executed instead.

    And here's a simple example that demonstrates how this loop can be used:


    <html> <head> <basefont face="Arial"> </head> <body> <center>Turning The Tables, PHP-Style!</center> <br> <? // define the number $number = 7; // use a for loop to calculate tables for that number for ($x=1; $x<=15; $x++) { echo "$number X $x = " . ($number*$x) . "<br>"; } ?> </body> </html>
    Let's dissect this a little bit:

    The first thing we've done here is define the number to be used for the multiplication table; we've used 7, since we're particularly poor at math - you might prefer to use another number.

    Next we've constructed a "for" loop with $x as the counter variable - we've initialized it to 1 and specified that the loop should run no more than 15 times.

    The $x++ you see in the "for" statement is an interesting little operator known as the auto-increment operator - more on this in the "Miscellaneous Notes" section below. For the moment, all you need to know is that it automatically increments the counter by 1 every time the loop is executed.

    Finally, the line that does all the work - the "echo" statement, which takes the number specified, multiplies it by the current value of the counter, and displays the result on the page.

    As you can see, a "for" loop is a very interesting - and useful - programming construct. Our next example illustrates its usefulness in a manner that should endear it to any HTML programmer.


    <html> <head> <basefont face="Arial"> </head> <body> <center>Turning The Tables, Part II</center> <br> <table border=1> <? // the purpose of this exercise is // to generate a 4x4 grid via an html table // this is accomplished via two nested "for" loops // the first one is for the table rows or <tr>s // we need four of them for ($alpha=1; $alpha<=4; $alpha++) { ?> <tr> <? // the second one is for the cells in each row or <td>s // we need four of them too for ($beta=1; $beta<=4; $beta++) { ?> <td> <? // print the coordinate of each cell echo("row $alpha, column $beta"); ?> </td> <? } ?> </tr> <? } ?> </table> </body> </html>
    And here's the output:

    <html> <head> <basefont face="Arial"> </head> <body> <center>Turning The Tables, Part II</center> <br> <table border=1> <tr> <td> row 1, column 1 </td> <td> row 1, column 2 </td> <td> row 1, column 3 </td> <td> row 1, column 4 </td> </tr> <tr> <td> row 2, column 1 </td> <td> row 2, column 2 </td> <td> row 2, column 3 </td> <td> row 2, column 4 </td> </tr> <tr> <td> row 3, column 1 </td> <td> row 3, column 2 </td> <td> row 3, column 3 </td> <td> row 3, column 4 </td> </tr> <tr> <td> row 4, column 1 </td> <td>s row 4, column 2 </td> <td> row 4, column 3 </td> <td> row 4, column 4 </td> </tr> </table> </body> </html>
    As you'll see if you try coding the same thing by hand, PHP's "for" loop just saved you a whole lot of work!

     
     
    >>> More PHP Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT