PHP Loops

PHP Loops


When writing code, it's common to want the same block of code to execute a specific number of times. We may utilize loops instead of adding roughly equivalent lines of code to a script.

Loops are used to repeatedly execute the same code block, provided that a given condition is true.

In PHP, we have the following loop types :

  • for - Loops over a section of code a certain number of times.
  • while - While the supplied condition is true, it loops over a block of code.
  • do...while - Once the block of code is looped, the loop will be repeated until the given conditions are correct.
  • foreach - For each entry in an array, it loops over a code block.
You can also search for these topics, php loops, types of loops in php, php loop through array, php loop example, php loop statement.


PHP for Loop

The for loop- Loops over a section of code a certain number of times.

When you know how many times you want the script to run, you should use for loop.

Syntax :-

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

Parameters :-

  • init counter: Set the loop counter value to a reasonable value.
  • test counter: Every loop iteration is evaluated. The loop will continue if it returns TRUE. The loop is terminated if its condition is False.
  • increment counter: This function raises the value of the loop counter.

Example 1 :- The example below displays the numbers from 1 to 5 :

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

Output :-

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

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.

Example 2 :- This example counts to 50 by tens :

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

Output :-

The number is: 10
The number is: 20
The number is: 30
The number is: 40
The number is: 50

Example Explained :-

  • $x = 10; - Initialize the loop counter ($x), and set the start value to 10.
  • $x <= 50; - Continue the loop as long as $x is less than or equal to 50.
  • $x+=10 - Increase the loop counter value by 10 for each iteration.
You can also search for these topics, php for loop, php for loop array, php for loop date range, php for loop syntax, php for loop example, php for loop patterns, php for loop break, php for loop parameters.

PHP while Loop

While the supplied condition is true, it loops over a block of code.

A while loop performs a coding block as long as the conditions stated are true.

Note:- It supports any user defined conditions.

Syntax :-

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

Example:- The example below displays the numbers from 1 to 5.

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

Output :-

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

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
You can also search for these topics, php while loop, php while loop array, php while loop example, php while loop multiple conditions, php while loop index, php while loop syntax, php while loop inside while loop.

PHP do while Loop

The do while loop - Once the block of code is looped, then the loop will be repeated until the given conditions are correct.

The do while loop will always perform the code block once, it checks the condition and repeats the loop until the condition specified is true.

Syntax :-

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

Example 1 :-

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

Output :-

The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5

The above example below first sets a variable $x to 1 ($x = 1). Then, the do while loop will write some output, and then increment the variable $x with 1. Then the condition is checked (is $x less than, or equal to 5?), and the loop will continue to run as long as $x is less than, or equal to 5:

Note :- In a do while loop, the condition is verified AFTER the loop's statements have been executed. This indicates that the do while statement will execute its statements at least once, even if the condition is false.

Example 2 :- This example sets the $x variable to 6, then it runs the loop, and then the condition is checked :

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

Output :-

The number is: 6



You can also search for these topics, php do while loop, php do while loop example, php do while array, php do while loop mulitiple example, php while infinite loop, php do while loop syntax.

PHP foreach Loop

Theforeach - For each entry in an array, it loops over a code block.

The foreach loop is used to cycle through each key/value pair in an array and is only applicable to arrays.

Syntax :-

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

The value of the actual array element is assigned to $value for each loop iteration, and the array indicator is shifted one way until the last array element is reached.

Example 1 :- The following example will output the values of the given array ($colors) :

<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
  echo "$value <br />";
}
?>

Output :-

red
green
blue
yellow

You can also search for these topics, php foreach loop, php foreach loop example, php for each loop syntax, php foreach last loop, php foreach inside while loop, php for loop break and continue, php for each loop index.

PHP break Statement

The break keyword is used exit from current loop or iteration in a looping statements.

Breaking out of a loop is also possible with the break statement.

It can be used with for, while, switch, and so on.

Example:- This example jumps out of the loop when x is equal to 2 :

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

Output :-

The number is: 0
The number is: 1



You can also search for these topics, php break statement, php break statement example, php break all loops, php break if, php break in while loop, php break vs continue, php break tag, php break loop.

PHP continue Statement

The continue keyword is used to skip one or more iteration in a looping statement.

If a certain condition happens, the continue statement breaks one or multiple iterations (in a loop) and continues with the remaining iteration in the loop.

Example :- This example skips the value of 2 :

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

Output :-

The number is: 0
The number is: 1
The number is: 3
The number is: 4

You can also search for these topics, php continue statement, php continue example, php continue nested loop, php continue for loop, php continue in all loops, php continue in while loop.