+91 9952732367

PHP Loops

PHP Loops

In PHP, we have the following looping statements:

  • while -  It tells PHP to execute the nested statement(s) repeatedly, as long as the while expression evaluates to TRUE.
  • do…while – do-while loops are very similar to while loops, except the truth expression is checked at the end of each iteration instead of in the beginning. The main difference from regular while loops is that the first iteration of a do-while loop is guaranteed to run
  • for - loops through a block of code a specified number of times
  • foreach - loops through a block of code for each element in an array

while loop

Syntax:

<?php
while (expr)
    statement
//or
while (expr){
    statement
}
//or
while (expr):
    statement
    ...
endwhile;
?>

Example:

<?php

$i = 1;
while ($i <= 10) {
echo $i++;  /* the printed value would be
$i before the increment
(post-increment) */
}

?>

do..while loop
Syntax:

<?php
do
{
 statement
}
while (expr);
?>

Example:

<?php
$i = 0;
do {
echo $i;
} while ($i > 0);
/*The above loop would run one time exactly, since after the first iteration, when truth expression is checked, it evaluates to <strong><code>FALSE</code></strong> (<var><var>$i</var></var> is not bigger than 0) and the loop execution ends.*/

$x=1;
do
{
echo "The number is: $x <br>"; // It display 1 to 5
$x++;
}
while ($x<=5)

?>

for loop
Syntax:

for (expr1; expr2; expr3)
    statement

/*The first expression (<var>expr1</var>) is evaluated (executed) once unconditionally at the beginning of the loop.

In the beginning of each iteration, <var>expr2</var> is evaluated. If it evaluates to <strong><code>TRUE</code></strong>, the loop continues and the nested statement(s) are executed. If it evaluates to<strong><code>FALSE</code></strong>, the execution of the loop ends.

At the end of each iteration, <var>expr3</var> is evaluated (executed). */

Example:

<?php
//display the numbers 1 through 10

/* example 1 */

for ($i = 1; $i <= 10; $i++) {
echo $i;
}

/* example 2 */

for ($i = 1; ; $i++) {
if ($i > 10) {
break;
}
echo $i;
}

/* example 3 */

$i = 1;
for (; ; ) {
if ($i > 10) {
break;
}
echo $i;
$i++;
}

/* example 4 */

for ($i = 1, $j = 0; $i <= 10; $j += $i, print $i, $i++);
?>

foreach loop:
Syntax:

foreach (array_expression as $value)
    statement
foreach (array_expression as $key => $value)
    statement

Example:

<?php
/* foreach example 1: value only */

$a = array(1, 2, 3, 17);

foreach ($a as $v) {
echo "Current value of $a: $v.n";
}

/* foreach example 2: value (with its manual access notation printed for illustration) */

$a = array(1, 2, 3, 17);

$i = 0; /* for illustrative purposes only */

foreach ($a as $v) {
echo "$a[$i] => $v.n";
$i++;
}

/* foreach example 3: key and value */

$a = array(
"one" => 1,
"two" => 2,
"three" => 3,
"seventeen" => 17
);

foreach ($a as $k => $v) {
echo "$a[$k] => $v.n";
}

/* foreach example 4: multi-dimensional arrays */
$a = array();
$a[0][0] = "a";
$a[0][1] = "b";
$a[1][0] = "y";
$a[1][1] = "z";

foreach ($a as $v1) {
foreach ($v1 as $v2) {
echo "$v2n";
}
}

/* foreach example 5: dynamic arrays */

foreach (array(1, 2, 3, 4, 5) as $v) {
echo "$vn";
}
?>
Copyright © s3learn.com