Search results
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++; }
break ends execution of the current for, foreach, while, do-while or switch structure. break accepts an optional numeric argument which tells it how many nested enclosing structures are to be broken out of.
while loops are the simplest type of loop in PHP. They behave just like their C counterparts. The basic form of a while statement is: statement. The meaning of a while statement is simple. It tells PHP to execute the nested statement (s) repeatedly, as long as the while expression evaluates to true.
Using PHP break statement in a while loop. The following example shows how to use the break statement in a while loop: $k = 0; while ($k <= 10) { if ($k === 5) { break; } echo "$k\n"; $k++; } Code language: PHP (php) Output: 0 1 2 3 4. This example also displays five numbers from 0 to 4. In each iteration, the variable $k is increased by one ...
The PHP Break Statement tutorial explains how two use two simple break execution of the current for, foreach, while, do-while or switch structure.
In this tutorial, you will learn how to use a break statement in PHP to come out from a loop. PHP break statement breaks the execution of current loops like while, do-while, for, foreach prematurely. If you specify break keyword in the inner loop, it will break the execution of the inner loop only.
The 'break' statement in PHP is used to terminate the execution of the entire switch statement. It is used to pass control to the next iteration of the enclosing foreach, for, while, do-while, or switch structure.