Wednesday, June 15, 2016

break statement

The break statement, though, enables you to break out of a code block.
Usually we use this in switch..case block. But we can use in loops as well as in the example below:

<?php
$i = -4;
for ($i ; $i <= 10; $i++ ) {
if ( $i == 0 )
break;
$temp = 1000/$i;
print "1000 divided by $i is... $temp<br>";
}
?>

save it as break.php and open from your browser.
We use break statement if we want to get out of a code block for probably to get rid of logic error such as division by zero or anything else.

The result should be like this:

1000 divided by -4 is... -250
1000 divided by -3 is... -333.33333333333
1000 divided by -2 is... -500
1000 divided by -1 is... -1000

The loop stop when $i equals to 0 (zero) and the loop gets out of the loop block.