Programming Basics

  Home arrow Programming Basics arrow Page 4 - 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 - For Loops


    (Page 4 of 5 )

    For loops offer a more restricted way of setting up code for repetitive execution. For loops begin with the keyword FOR and then a set of 3 statements: a variable set to an initial value to be used as the loop's counter, the conditional statement and then the increment in which the counter value will be adjusted after each execution. The code block is bracketed and follows the for statement.

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

    In this example, the $i variable serves the same purpose as our previous $donuts_remaining variable. It's initial value has been set to 12 and the code block will be processed until it's value is no longer greater than 0. Each time the block is executed, $i will be decreased by one.

    For loops are more restricted in that the variable is intended only to act as a counter mechanism. It wouldn't be good practice to change it's value in within the code block! This is compared to the do-while and while loops where the value is intentionally changed in the code block to affect the loop's behavior.

    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 2 - Follow our Sitemap