PHP Break

0
5K

The break statement can be used to jump out of different kind of loops.


Break in For loop

The break statement can be used to jump out of a for loop.

ExampleGet your own PHP Server

Jump out of the loop when $x is 4:

for ($x = 0; $x < 10; $x++) {
  if ($x == 4) {
    break;
  }
  echo "The number is: $x <br>";
}
Try it Yourself »

Break in While Loop

The break statement can be used to jump out of a while loop.

Break Example

$x = 0;

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


Break in Do While Loop

The break statement can be used to jump out of a do...while loop.

Example

Stop the loop when $i is 3:

$i = 1;

do {
  if ($i == 3) break;
  echo $i;
  $i++;
} while ($i < 6);
Try it Yourself »

Break in For Each Loop

The break statement can be used to jump out of a foreach loop.

Example

Stop the loop if $x is "blue":

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

foreach ($colors as $x) {
  if ($x == "blue") break;
  echo "$x <br>";
}
Try it Yourself »
Cerca
Categorie
Leggi tutto
Altre informazioni
PHP Math
PHP has a set of math functions that allows you to perform mathematical tasks on numbers. PHP...
By PHP Tutorial 2024-05-17 07:39:09 0 6K
Altre informazioni
PHP - Concatenate Strings
String Concatenation To concatenate, or combine, two strings you can use...
By PHP Tutorial 2024-05-17 07:30:24 0 8K
Altre informazioni
PHP Indexed Arrays
PHP Indexed Arrays In indexed arrays each item has an index number. By default, the first item...
By PHP Tutorial 2024-05-17 08:00:38 0 5K
Altre informazioni
PHP Continue
The continue statement can be used to jump out of the current iteration of a loop, and...
By PHP Tutorial 2024-05-17 07:52:32 0 4K
Altre informazioni
PHP while Loop
The while loop - Loops through a block of code as long as the specified condition is...
By PHP Tutorial 2024-05-17 07:48:58 0 4K
Sociallez https://sociallez.com