Logo

Looping in Php

Posted under » PHP on 13 April 2009

Very often when you write code, you want the same block of code to run a number of times. You can use looping statements in your code to perform this.

The do...while Statement

<¿php 
$i=0;
do
  {
  $i++;
  echo "The number is " . $i . "
"; } while ($i<5); ?>

The for statement is the most advanced of the loops in PHP.

<¿php 
for ($i=1; $i<=5; $i++)
{
  echo "Hello World!
"; } ?>

The foreach statement is used to loop through arrays.

<¿php
$arr=array("one", "two", "three");

foreach ($arr as $value)
{
  echo "Value: " . $value . "
"; } ?>