PHP Break


 

PHP Break

In this tutorial we will study about break statement, break statement is generally used when need to discontinue or break a loop. Examples in this tutorial will make it more clear.

In this tutorial we will study about break statement, break statement is generally used when need to discontinue or break a loop. Examples in this tutorial will make it more clear.

Break Control Structure:

Break is a special kind of control structure which helps us to break any loop or sequence, like if we want to break the flow of any loop (for, while etc) then we can use break statement, generally we need if control structure to implement .

If you want to break multiple control structure then we need to provide numeric argument along with break statement, example is given below:

Example:

<?php

for($a=1;$a<10;$a++)

{

if($a==5)

break;

else

echo "$a\t";

}

?>

Output:

1 2 3 4

Example:

<?php

$arr=array(1,2,3,4,5);

foreach($arr as $val):

if($val==4)break;

echo $val."<br/>";

endforeach;

?>

Output:

1
2
3

Ads