Php Break statement is used to breaks one loop if a specified condition is true continue with the next loop.
Here is the code of php break statement.
Code | |||||||
---|---|---|---|---|---|---|---|
<?php for($i=1;$i<=10;$i++){ if($i==6){ break; } echo $i . " |
Type Url - https://localhost/php/break.php
Output | |||||||
---|---|---|---|---|---|---|---|
1 2 3 5 |
Break Without <br> tag will give output in same line called as inline.
Code | |||||||
---|---|---|---|---|---|---|---|
<?php for($i=1;$i<=10;$i++){ if($i==6){ break; } echo $i; } ?> |
Type Url - https://localhost/php/break1.php
Output | |||||||
---|---|---|---|---|---|---|---|
12345 |
Below code show you how to use break in while loop
Code | |||||||
---|---|---|---|---|---|---|---|
<?php $i=1; while($i<=7){ if($i==4){ break; } echo $i . " |
Type Url - https://localhost/php/while-break.php
Output | |||||||
---|---|---|---|---|---|---|---|
1 2 3 |