Programming Basics
  Home arrow Programming Basics arrow Page 2 - 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 - While Loops


    (Page 2 of 5 )

    The while loop sets off a section of code to repeat with braces. It uses the keyword WHILE followed by a condition when the looping should stop. Here's a basic example:

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

    We first assign the value of 12 to the variable $remaining_donuts. Then, for each donut that's eaten, a message is outputted and the value of $remaining_donuts is decremented by one.

    The section of code is not processed after the value of $remaining_donuts is no longer greater than 0. The normal flow of the script continues past the code block and a concluding "I ate them all!" message is outputted.

    It's important to be careful when constructing a looping condition and when adjusting the variables' values in the code block. For example, code which adjusts the value of $remaining_donuts incorrectly would loop indefinitely!

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

    Note the while loop evaluates the condition before it executing the code block. If the statement resolves false then the loop's code is skipped and the flow of the script continues afterwards. If the value of $remaining_donuts was set to 0 or a negative number, for example, the braced code will not be executed and the script will simply display "I ate them all!"

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

    Conditional expressions is beyond the intended scope of this tutorial, so if you need assistance with comparison operators and how PHP makes decisions, I recommend my previous tutorial, A Tour of Decision Making Structures in PHP.

    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-2010 by Developer Shed. All rights reserved. DS Cluster 8 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek