Programming Basics
  Home arrow Programming Basics arrow Page 3 - Loops and PHP Decision Making
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  
Forums Sitemap 
Dedicated Servers  
Download TestComplete 
JMSL Numerical Library 
IBM® developerWorks
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

Loops and PHP Decision Making
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2007-10-18

    Table of Contents:
  • Loops and PHP Decision Making
  • Breaking out
  • Looping
  • for Loops
  • Breaking Out of a Loop

  • 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


    Loops and PHP Decision Making - Looping


    (Page 3 of 5 )

     

    Now that you've changed the flow of your PHP program based on comparisons, you need to learn that if you want to repeat a task until a comparison is FALSE, you'll need to use looping. Each time the code in the loop executes, it is called an iteration. This is useful for many common tasks, such as displaying the results of a query by looping through the returned rows. PHP provides the while, for, and do ... while constructs to perform loops.

    Each of the loop constructs requires two basic pieces of information. First, the condition to stop looping is defined just like the comparison in an if statement. Second, the code to perform is also required and specified either on a single line or within curly braces. A logical error would be to omit the code from a loop that relies on the code executed to cause the loop to stop, causing an infinite loop.

    The code is executed as long as the expression evaluates to TRUE. To avoid an infinite loop, which would loop forever, your code should have the expressions eventually become FALSE. When this happens, the loop stops, and execution continues with the next line of code, following the logical loop.

    while Loops

    The while loop takes the expression followed by the code to execute. Figure 4-3 illustrates how a while loop processes. 


    Figure 4-3.  How a while loop executes

    The syntax for a while loop is:

      while (expression)
      {
        code to execute;
      }

    An example is shown in Example 4-14.

    Example 4-14. A sample while loop that counts to 10

    <?php
    $num = 1;

    while ($num <= 10){
       
    print "Number is $num<br />";
       
    $num++;
    }

    print 'Done.';
    ?>

    Example 4-14 produces the following:

      Number is 1
      Number is 2
      Number is 3
      Number is 4
      Number is 5
      Number is 6
      Number is 7
      Number is 8
      Number is 9
      Number is 10
      Done.

    Before the loop begins, the variable $num is set to 1. This is called initializing a counter variable. Each time the code block executes, it increases the value in $num by 1 with the statement $num++;. After 10 iterations, the evaluation $num <= 10 becomes FALSE, then the loop stops and it prints Done. Be sure to increase the $num var, as the while loop depends on it.

    Be careful not to create an infinite loop. It has the undesirable effect of not returning your page and taking a lot of processing time on the web server.

    do ... while Loops

    The do ... while loop takes an expression such as a while statement but places it at the end. The syntax is:

      do {
        code to execute;
      } while (expression);

    This loop is useful when you want to execute a block of code at least once regardless of the expression value. For example, let's count to 10 with this loop, as shown in Example 4-15.

    Example 4-15. Counting to 10 with do ... while

    <?php

    $num = 1;

    do {
       
    echo "Number is ".$num."<br />";
       
    $num++;
    } while ($num <= 10);

    echo "Done.";

    ?>

    Example 4-15 produces the same results as Example 4-14; if you change the value of $num to 11, the loop processes differently:

      <?php

      $num = 11;

      do {
          echo $num;
          $num++;
      } while ($num <= 10);

      ?>

    This produces:

      11

    The code in the loop displays 11 because the loop always executes at least once. Following the pass, while evaluates to FALSE, causing execution to drop out of the do ... while loop.

    More Programming Basics Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Learning PHP and MySQL, Second Edition,"...
     

    Buy this book now. This article is excerpted from chapter four of the book Learning PHP and MySQL, Second Edition, written by Michele Davis and Jon Phillips (O'Reilly, 2006; ISBN: 0596101104). Check it out today at your favorite bookstore. Buy this book now.

    PROGRAMMING BASICS ARTICLES

    - 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
    - An Overview of Arrays in PHP






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway