PHP Break

0
2Кб

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 »
Поиск
Категории
Больше
Другое
PHP Installation
What Do I Need? To start using PHP, you can: Find a web host with PHP and MySQL support...
От PHP Tutorial 2024-05-17 07:09:40 0 2Кб
Другое
PHP Numbers
In this chapter we will look in depth into Integers, Floats, and Number Strings. PHP Numbers...
От PHP Tutorial 2024-05-17 07:37:05 0 3Кб
Другое
PHP Strings
A string is a sequence of characters, like "Hello world!". Strings Strings in PHP are...
От PHP Tutorial 2024-05-17 07:28:17 0 3Кб
Другое
PHP Syntax
A PHP script is executed on the server, and the plain HTML result is sent back to the browser....
От PHP Tutorial 2024-05-17 07:10:53 0 3Кб
Другое
PHP Casting
Sometimes you need to change a variable from one data type into another, and sometimes you want a...
От PHP Tutorial 2024-05-17 07:38:02 0 2Кб