Programming Basics

  Home arrow Programming Basics arrow Page 2 - Looping in PHP
PROGRAMMING BASICS

Looping in PHP
By: bluephoenix
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 4
    2004-08-19

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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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

    blog comments powered by Disqus

    PROGRAMMING BASICS ARTICLES

    - Control Flow Constructs
    - More Time Manipulation with PHP
    - Validating and Manipulating Dates with PHP
    - Using the Date Constructor in PHP
    - Calendar Construction with PHP
    - PHP`s Calendar Package
    - Getting Modified Versions and Correct Dates ...
    - Combining Date Functions in PHP
    - Using PHP for Date and Time in Programming
    - More Exception Handling with PHP
    - Exception Handling in PHP
    - Error Logging and Handling Exceptions
    - Configuration Directives for Error and Excep...
    - Error and Exception Handling
    - Python Modules for Games


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap