Amazon.com: PC Books || Educational Software || Magazines
Amazon: Books-CA || Software-CA || Books-UK || Software-UK

HTML | Javascript | ASP | PHP | VBScript | SQL | Hardware | PC FAQ| WinXP|

Learn to Build, Upgrade, or Repair your Computer Ebook + PC Safety 101 kwwebservice.com Reasonably priced web development & hosting
PHP Basics
Introduction
Variables & Arrays
Sub & Functions
If Statements
Case Statements
Loop Statements
Date Objects
Form Processing
Text File Objects
Database Access
Adding Record
Updating Record
Deleting Record
Virtual Includes
Cookies
Session
Server Variables
Creating Login Page
E-mail
PHP Books @
CA Amazon
UK Amazon
US Amazon

  :: Home
PC Topics
  :: Build A PC
  :: Windows XP
  :: PC Help
Tutorials
  :: HTML
  :: JavaScript
  :: ASP
  :: PHP
  :: VBScript
  :: SQL
Miscellaneous
  :: Code/Scripts
  :: Forum
  :: Links
  :: Contact us
  :: Tell A Friend
  :: In Somali
  ::
Web Articles
  Top Web Hosting
Reviews
  Processing
Online Card Payment Guide
  Domain name
registration & buying guide
  Getting
a website online guide
  Search Engine
submission & optimization tips
Netfirms Web Hosting
Free trial
Quality business correspondence.
Yahoo! Search Marketing


PHP Loop Statements

Loops are set of instructions that repeat elements in specific number of times. Counter variable is used to increment or decrement with each repetition of the loop. The two major groups of loops are, For..Next and While..Loop. The For statements are best used when you want to perform a loop in specific number of times. The Do and While statements are best used to perform a loop an undetermined number of times.
This is a For..Next example which counts from 1 to 5.
<?php
for($x=1;$x<6;$x++)
{
   print("The x is: ".$x."<br>");
}
?>
Execution result:
The x is: 1
The x is: 2
The x is: 3
The x is: 4
The x is: 5

The above example increments a variable counter from 1 to 5 The values of x are incremented by 1 on each run before 6.
The following example will multiply value of x in above example by 2 on each run.
<?php
for($x=1;$x<6;$x++)
{
   print("The value of $x by 2 is: ".($x*2)."<br>");
}
?>
The value of 1 by 2 is: 2
The value of 2 by 2 is: 4
The value of 3 by 2 is: 6
The value of 4 by 2 is: 8
The value of 5 by 2 is: 10

While and Do While

The Do..While structure repeats a block of statements until a specified condition is met.  While performs a loop statement as long as the condition being tested is true. Each run of the code block in a loop is called an iteration.  

The following is a example of While loop statement to accomplish previous example:
<?php
$x=1;
while ($x<6)
{
   print"The x is: ".$x."<br>";
   $x++;
}
?>
Execution result:
The x is: 1
The x is: 2
The x is: 3
The x is: 4
The x is: 5
You can also accomplish the loop this way by using Do..While:
<?php
$x=1;
Do {
   print"The x is: ".$x."<br>";
   $x++;
} while($x<6)
?>
Execution result:
The x is: 1
The x is: 2
The x is: 3
The x is: 4
The x is: 5

Nested Loop Statement

A nested loop statement is a loop within a loop. It's usefull when accomplishing parent/child related records similar to car and it's inforation as wes seen in the array session.
The following example is using 2 demmitional arrays and nested loop statement:
<?php
$car1[0][1]="Jeep Grand Cherokee";
$car1[0][2]=2002;
$car1[0][3]="$14,900";
$car1[1][1]="Jeep Wrangler";
$car1[1][2]=2002;
$car1[1][3]="$11,852";
$car1[2][1]="Jeep Liberty";
$car1[2][2]=2004;
$car1[2][3]="$29,955";
$car1[3][1]="Jeep Cherokee Briarwood";
$car1[3][2]=2005;
$car1[3][3]="$30,000";
for($x=0; $x<4;$x++)
{
for($i=1; $i<4;$i++)
{
echo $car1[$x][$i]."<br>";
}
echo"<br>";
}
?>
Execution result:
Jeep Grand Cherokee
2002
$14,900

Jeep Wrangler
2002
$11,852

Jeep Liberty
2004
$29,955

Jeep Cherokee Briarwood
2005
$30,000

Case Statements Form Processing
Listed @ ConsumerVote.com - The Consumer Rated Web Directory
PC Articles
  Computer Safety
prevent viruses & Spyware