PHP Break
Posté 2024-05-17 07:50:37
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 »Rechercher
Catégories
- Art
- Causes
- Crafts
- Dance
- Drinks
- Film
- Fitness
- Food
- Jeux
- Gardening
- Health
- Domicile
- Literature
- Music
- Networking
- Autre
- Party
- Religion
- Shopping
- Sports
- Theater
- Wellness
Lire la suite
PHP - Slicing Strings
Slicing
You can return a range of characters by using the substr() function.
Specify...
PHP Multidimensional Arrays
In the previous pages, we have described arrays that are a single list of key/value pairs....
PHP $GLOBALS
$GLOBALS is an array that contains all global variables.
Global Variables
Global...
PHP Update Array Items
Update Array Item
To update an existing array item, you can refer to the index number for...
PHP Variables
Variables are "containers" for storing information.
Creating (Declaring) PHP Variables
In...