PHP
  Home arrow PHP arrow Page 3 - 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 - Revisiting The Past
    ( Page 3 of 9 )

    So that's the "while" loop - it executes a set of statements until a specified condition is satisfied. But what happens if the condition is satisfied on the first iteration itself? In the illustration above, for example, if you were to enter the year 2000, the "while" loop would not execute even once. Try it yourself and you'll see what we mean.

    So, if you're in a situation where you need to execute a set of statements *at least* once, PHP offers you the "do-while" loop. Here's what it looks like:

    do { do this! } while (condition) Let's take a quick example: <?php $bingo = 366; while ($bingo == 699) { echo "Bingo!"; break; } ?>
    In this case, no matter how many times you run this PHP script, you will get no output at all, since the value of $bingo is not equal to 699. But, if you ran this version of the script


    <?php $bingo = 799; do { echo "Bingo!"; break; } while ($bingo == 699); ?>
    you'd get one line of output.

    Thus, the construction of the "do-while" loop is such that the statements within the loop are executed first, and the condition to be tested is checked after. This implies that the statements within the curly braces would be executed at least once.

    Let's modify the time machine above to use a "do-while" loop instead:


    <? // 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>"; // in this case, the "do-while" loop // ensures that at least one line of output is generated // even when you enter the year 2000 do { $current = $current - 1; echo "$current "; } while($year < $current); echo "<br><b>The past definitely isn't all it's cracked up to be!</b>"; } ?> </body> </html> <? } ?>


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

       

    PHP ARTICLES

    - 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 ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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