Programming Basics
  Home arrow Programming Basics arrow Page 3 - Looping in PHP
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
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? 
PROGRAMMING BASICS

Looping in PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2004-08-19

    Table of Contents:
  • Looping in PHP
  • While Loops
  • Do-While Loops
  • For Loops
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Looping in PHP - Do-While Loops


    (Page 3 of 5 )

    I'll keep the donut theme as we move on to do-while loops. Do-while loops start with the keyword DO and conclude with the keyword WHILE followed by the conditional expression. Between DO and WHILE exists the braced set of loop code.

    <?php
    $remaining_donuts 
    12;
    do {
      echo 
    "I'm eating a donut...&gt;br /&lt;";
      
    $remaining_donuts--;
    } while (
    $remaining_donuts &gt0);
    echo 
    "I ate them all!\n";
    ?>

    TThe output of this example is the same as the first while loop example shown earlier. 12 is assigned to the variable $remaining_donuts. For each donut consumed a message is outputted and $remaining_donuts is decremented. The code block is no longer processed once the value of the conditional resolves false. A concluding "I ate them all!" message is outputted.

    The core difference between the do-while and the while loop is when the conditional expression is evaluated. Do-while loops do not evaluate the expression until after executing the code block at least once. The flow of the script falls into the braced code and then tests if it should repeat the section again.

    A do-while loop on the other hand will process its code bock at least once, as lustrated with the following code:

    <?php
    $remaining_donuts 
    = -1;
    do {
      echo 
    "I'm eating a donut...&lt;br /&gt;";
      
    $remaining_donuts--;
    } while (
    $remaining_donuts &gt0);
    echo 
    "I ate them all!";
    ?>

    Even though the value of $remaining_donuts has been set to a negative value, our hungry program still manages to find something to eat before admitting it's gluttony!

    When to use a do-while loop or a while loop depends on the logic of your code... If code may or may not be looped depending on a condition, you should use a while loop because its conditional expression is tested first. If code must be executed at least once but future loops are then dependent on the evaluation of a conditional expression, use a do-while loop because the expression is tested after the first execution.

    More Programming Basics Articles
    More By bluephoenix


       · A good rule of thumb for when to use for vs while loops is this. If you know how...
     

    PROGRAMMING BASICS ARTICLES

    - PHP: Hypertext Preprocessor: What is it?
    - Loops and PHP Decision Making
    - Operators, Conditionals, and PHP Decision-Ma...
    - PHP Decision-Making
    - Coding
    - Server Statistics
    - Looping in PHP
    - Cookies in PHP
    - Working with text files
    - Beginning Object Oriented Programming in PHP
    - A Tour of Decision Making Structures in PHP
    - PHP Strings Primer
    - PHP Control Structures
    - Intro to Vim
    - Reading Directorys with PHP





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