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


       · 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek