PHP – Loops

0 0
Read Time:3 Minute, 8 Second

PHP – Loops

Loops are used to execute the same block of code again and again, as long as a certain condition is true.

Types of loops:

  • for
  • while
  • do-while
  • foreach

Important features of loops:

  • continue
  • break (you’ve already seen this used with switch)

PHP -The for Loop

Use this type of loop when you know how many times you want it to run.

Example:

<?php
for ($x = 1; $x <= 10; $x++) {
  echo "The CodeDixa<br/>";
}

Output:

The CodeDixa
The CodeDixa
The CodeDixa
The CodeDixa
The CodeDixa
The CodeDixa
The CodeDixa
The CodeDixa
The CodeDixa
The CodeDixa
  • The first expression (initialization) is evaluated (run) once unconditionally at the beginning of the loop.
  • At the start of each loop (or iteration), condition is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are run. if it evaluates FALSE, the execution of loop ends.
  • At the end of each iteration, the post-op condition, in this example an increment is evaluated (run).
  • Each of the expression can be empty or contain multiple expressions separated by commas.

PHP -The while Loop

Use this type of loop when you want the condition to be tested before the commands are run.

Example:

<?php

$i=1;
while ( $i <= 5 ) {
  echo "The CodeDixa <br>";
  $i++;
}

?>

Output:

The CodeDixa
The CodeDixa
The CodeDixa
The CodeDixa
The CodeDixa
  • At the start of each loop (or iteration), condition is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are run. If it evaluates to FALSE, the the execution of the loop ends. So this statement might never run.
  • The big difference between for and while is for runs first, then tests and while tests first, then if the condition is true, then it runs.
  • At the end of each iteration, increment is evaluated (run).

PHP -The do..while Loop

A variation of a while statement, do..while allows you to run the code once, then check the condition

Example:

<?php
$i=1;
do 
{
  $i++;
  echo "The CodeDixa <br>";
} while ( $i <= 5 );
?>

Output:

The CodeDixa
The CodeDixa
The CodeDixa
The CodeDixa
The CodeDixa
  • At the start of each loop (or iteration), condition is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are run. If it evaluates to FALSE, the execution of loop ends. This statements is always run at least once, but may not repeat depending on the ending conditional in the while statement.
  • At the end of each iteration, increment is evaluated (run).

PHP -The foreach Loop

foreach works on arrays and is used to loop through each key/value pair in an array.

Example:

<?php
$colors = array("red", "green", "blue", "yellow");

foreach ($colors as $value) {
  echo "$value <br>";
}

/* Output:
red
green
blue
yellow
*/

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $val) {
  echo "$x = $val<br>";
}

/* Output:
Peter = 35
Ben = 37
Joe = 43
*/
?>

PHP -The continue statement

continue is used within looping structures (do, while, for, foreach) to skip the rest of the current loop iteration and go to the beginning of the next evaluation.

Example:

<?php
for ($i=0; $i<=5; $i++) {
    if($i==3) {
    continue;
    }
    echo "The number is ".$i;
    echo "<br/>";
}

/* Output:
The number is 0
The number is 1
The number is 2
The number is 4
The number is 5
*/

?>

PHP -The break statement

Break ends running of the current for, foreach, while, do-while or switch structure.

<?php
for ($i=0; $i<=5; $i++) {
    if($i==3) {
    break;
    }
    echo "The number is ".$i;
    echo "<br/>";
}

/* Output:
The number is 0
The number is 1
The number is 2
*/

?>
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

Leave a Comment