PHP Loops


PHP Loops

Usually when you write code, you want the same code block to run repeatedly a certain number of times. So, instead of adding a few lines of code that almost match the text, we can use loops.

Loops are used to use the same block of code many times, as long as the particular situation is true.

In PHP, we have the following loop types:

  • while - loops through a block of code as long as the specified condition is true
  • do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
  • 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



PHP while Loop



While loop - Connects to the code block as long as the specified condition is true.


The PHP while Loop

Loop while using the code block as long as the condition stated is true.

Syntax


while (condition is true) {
  code to be executed;
}

Examples

The example below shows the numbers from 1 to 5:


Example
<?php
$x = 1;

while($x <= 5) {
  echo "The number is: $x <br>";
  $x++;
}
?>

Example Explained

  • $x = 1; - Initialize the loop counter ($x), and set the start value to 1
  • $x <= 5 - Continue the loop as long as $x is less than or equal to 5
  • $x++; - Increase the loop counter value by 1 for each iteration

This example counts 100 in tens:


Example
<?php
$x = 0;

while($x <= 100) {
  echo "The number is: $x <br>";
  $x+=10;
}
?>

Example Explained

  • $x = 0; - Initialize the loop counter ($x), and set the start value to 0
  • $x <= 100 - Continue the loop as long as $x is less than or equal to 100
  • $x+=10; - Increase the loop counter value by 10 for each iteration



PHP do while Loop



Do...while loop - It goes into the code block once, and then repeats the loop as long as the specified condition is true.


The PHP do..while Loop

The do...while the loop will always use the code block once, then check the status, and repeat the loop when the specified state is true.

Syntax

do {
  code to be executed;
} while (condition is true);

Examples

The example below starts with setting the variable x x 1 ($ x = 1). Then, loop the do while it will record another output, and then raise the variable $ x per 1. Then the condition is checked ($ x less, or equal to 5?), And the loop will continue to work as long as $ x is less, or equal to 5:


Example
<?php
$x = 1;

do {
  echo "The number is: $x <br>";
  $x++;
} while ($x <= 5);
?>

Note: In practice do... while loop status is checked AFTER using statements within the loop. This means that do... while the loop will use its statements at least once, even if the situation is false. See an example below.


This example sets a variable of x x to 6, then uses a loop, and then the status is checked:


Example
<?php
$x = 6;

do {
  echo "The number is: $x <br>";
  $x++;
} while ($x <= 5);
?>




PHP for Loop



For Loop - A loop in a code block a certain number of times.


The PHP for Loop

The loop is used if you know in advance how often the script should work.

Syntax

for (init counter; test counter; increment counter) {
  code to be executed for each iteration;
}

Parameters
  • init counter: Initialize the loop counter value
  • test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
  • increment counter: Increases the loop counter value
Examples

The example below shows numbers from 0 to 10:


Example
<?php
for ($x = 0; $x <= 10; $x++) {
  echo "The number is: $x <br>";
}
?>

Example Explained

  • $x = 0; - Initialize the loop counter ($x), and set the start value to 0
  • $x <= 10; - Continue the loop as long as $x is less than or equal to 10
  • $x++ - Increase the loop counter value by 1 for each iteration

This example counts 100 in tens:


Example
<?php
for ($x = 0; $x <= 100; $x+=10) {
  echo "The number is: $x <br>";
}
?>

Example Explained

  • $x = 0; - Initialize the loop counter ($x), and set the start value to 0
  • $x <= 100; - Continue the loop as long as $x is less than or equal to 100
  • $x+=10 - Increase the loop counter value by 10 for each iteration



PHP foreach Loop



For-loop - Connects to each element code block to the same members.


The PHP foreach Loop

The forloop only applies to the same members, and is used to connect between each key / value pairing in the system.

Syntax


foreach ($array as $value) {
  code to be executed;
}

In each loop of the loop, the current element of the same members is assigned to the value of $ and the identifier of the same members is moved one by one, until it reaches the last half of the same members.

Examples

The following example will give you the colors of the same given members ($ colors):


Example

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

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

The following example will extract both the keys and the values ​​of the same given member ($ age):


Example

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

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



PHP Break and Continue




PHP Break

You have already seen the break statement used in the previous chapter of this study. Used for "skipping" on the switch statement.

The break statement can also be used to jump into a loop.

This pattern comes out of the loop when x is equal to 4:


Example
<?php
for ($x = 0; $x < 10; $x++) {
  if ($x == 4) {
    break;
  }
  echo "The number is: $x <br>";
}
?>


PHP Continue

The continue statement terminates one replica (loop), if the specified condition occurs, and continues the following repetition in loop.

This example exceeds 4 value:


Example
<?php
for ($x = 0; $x < 10; $x++) {
  if ($x == 4) {
    continue;
  }
  echo "The number is: $x <br>";
}
?>


Break and Continue in While Loop

You can also use break and continue while loops:


Break Example
<?php
$x = 0;

while($x < 10) {
  if ($x == 4) {
    break;
  }
  echo "The number is: $x <br>";
  $x++;
}
?>


Continue Example
<?php
$x = 0;

while($x < 10) {
  if ($x == 4) {
    $x++;
    continue;
  }
  echo "The number is: $x <br>";
  $x++;
}
?>