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 »
Pesquisar
Categorias
Leia Mais
Outro
PHP Installation
What Do I Need? To start using PHP, you can: Find a web host with PHP and MySQL support...
Por PHP Tutorial 2024-05-17 07:09:40 0 5K
Outro
PHP Math
PHP has a set of math functions that allows you to perform mathematical tasks on numbers. PHP...
Por PHP Tutorial 2024-05-17 07:39:09 0 6K
Outro
PHP Comments
Comments in PHP A comment in PHP code is a line that is not executed as a part of the program....
Por PHP Tutorial 2024-05-17 07:14:49 0 6K
Outro
PHP Functions
The real power of PHP comes from its functions. PHP has more than 1000 built-in functions, and...
Por PHP Tutorial 2024-05-17 07:53:06 0 4K
Outro
PHP - $_SERVER
$_SERVER $_SERVER is a PHP super global variable which holds information about headers,...
Por PHP Tutorial 2024-05-17 08:08:50 0 7K
Sociallez https://sociallez.com