PHP Break

0
5χλμ.

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 - Slicing Strings
Slicing You can return a range of characters by using the substr() function. Specify...
από PHP Tutorial 2024-05-17 07:31:23 1 8χλμ.
άλλο
PHP Create Arrays
Create Array You can create arrays by using the array() function: ExampleGet your...
από PHP Tutorial 2024-05-17 08:01:55 0 8χλμ.
άλλο
PHP Sorting Arrays
The elements in an array can be sorted in alphabetical or numerical order, descending or...
από PHP Tutorial 2024-05-17 08:06:46 0 8χλμ.
άλλο
PHP Data Types
PHP Data Types Variables can store data of different types, and different data types can do...
από PHP Tutorial 2024-05-17 07:27:24 0 5χλμ.
άλλο
الثورة البلوكتشين وتحولاتها في الصناعات المختلفة
مقدمة: شهدت تقنية البلوكتشين ثورة هائلة في السنوات الأخيرة، حيث أصبحت لديها القدرة على تغيير...
από MOHAMED ATTALLAH 2024-05-14 14:03:03 0 7χλμ.
Sociallez https://sociallez.com