PHP
  Home arrow PHP arrow Page 2 - 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? 
Google.com  
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 - Back To The Future
    ( Page 2 of 9 )

    For those of you unfamiliar with the term, a loop is a control structure which enables you to repeat the same set of PHP statements or commands over and over again; the actual number of repetitions may be dependent on a number you specify, or on the fulfillment of a certain condition or set of conditions.

    The first - and simplest - loop to learn in PHP is the so-called "while" loop, which looks like this:

    while (condition) { do this! }
    or, in English,

    while (it's raining) { carry an umbrella! }
    In this case, so long as the condition specified evaluates as true - remember what you learned last time? - the PHP statements within the curly braces will continue to execute. As soon as the condition becomes false - the sun comes out, for example - the loop will be broken and the statements following it will be executed.

    Here's a quick example which demonstrates the "while" loop:


    <? // if form has not been submitted, display initial page if (!$submit) { ?> <html> <head> </head> <body> <h2>The Incredible Amazing Fantabulous Time Machine</h2> <form action="tmachine.php4" method="POST"> Which year would you like to visit? <input type="text" name="year" size="4" maxlength="4"> <input type="submit" name="submit" value="Go"> </form> </body> </html> <? } else // else process it and generate a new page { ?> <html> <head> </head> <body> <? // current year $current = 2000; // check for dates in future and generate appropriate message if ($year > $current) { echo "<h2>Oops!</h2>"; echo "Sorry, this time machine can only travel backwards at the moment. But leave your phone number and we'll call you when the new improved model goes on sale."; } else { // or echo "<b>Going back to $year...</b><br>"; // use a while loop to print a series of numbers (years) // until the desired number (year) is reached while($year < $current) { $current = $current - 1; echo "$current "; } echo "<br><b>The past definitely isn't all it's cracked up to be!</b>"; } ?> </body> </html> <? } ?>
    In this case, we first ask the user for the year he'd like to visit - this year is stored as the variable $year, and passed to the PHP script. The script first checks the year to ensure that it in the past [hey, we're working on it!] and then uses a "while" loop to count backwards from the current year - 2000, stored in the variable $current - until the values of $current and $year are equal.

    Note our usage of the $submit variable to use the same PHP page to generate both the initial form and the subsequent pages - we explained this technique in detail last time.

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

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek