PHP Break

0
5KB

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 Magic Constants
PHP Predefined Constants PHP has nine predefined constants that change value depending on where...
Por PHP Tutorial 2024-05-17 07:40:16 0 5KB
Outro
PHP Operators
PHP Operators Operators are used to perform operations on variables and values. PHP divides the...
Por PHP Tutorial 2024-05-17 07:41:39 0 9KB
Outro
تطورات التعلم الآلي وتأثيرها على مستقبل العمل
مقدمة: في ظل التطورات السريعة للتكنولوجيا والابتكار، أصبح التعلم الآلي...
Por MOHAMED ATTALLAH 2024-05-14 13:56:58 0 7KB
Outro
PHP if Statements
Conditional statements are used to perform different actions based on different conditions....
Por PHP Tutorial 2024-05-17 07:45:23 0 4KB
Outro
PHP Variables
Variables are "containers" for storing information. Creating (Declaring) PHP Variables In...
Por PHP Tutorial 2024-05-17 07:16:59 0 7KB
Sociallez https://sociallez.com