PHP Break

0
2KB

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 »
Search
Nach Verein filtern
Read More
Other
PHP Create Arrays
Create Array You can create arrays by using the array() function: ExampleGet your...
Von PHP Tutorial 2024-05-17 08:01:55 0 3KB
Other
PHP Functions
The real power of PHP comes from its functions. PHP has more than 1000 built-in functions, and...
Von PHP Tutorial 2024-05-17 07:53:06 0 2KB
Other
PHP if...else Statements
PHP - The if...else Statement The if...else statement executes some code if a...
Von PHP Tutorial 2024-05-17 07:46:12 0 2KB
Other
PHP Add Array Items
Add Array Item To add items to an existing array, you can use the bracket [] syntax....
Von PHP Tutorial 2024-05-17 08:06:05 0 4KB
Other
PHP for Loop
The for loop - Loops through a block of code a specified number of times. The PHP...
Von PHP Tutorial 2024-05-17 07:49:43 0 2KB