PHP break statement

We use two jump statement in PHP such as break statement and continue statement. Break means stop. So PHP break statement stop the program execution based upon condition. We use PHP break and continue statement to control the loop or program flow. PHP break and continue statement is totally different from each others. If we found PHP break statement inside a loop then the loop will stop the execution and the loop will be terminated. So we can say that if PHP break statement is available in the loop then the loop won't work by comparing specific condition. In the condition part we use if statement and then we set break statement.

PHP break statement Structure:


if(condition)
break;

In this code at first we use a condition, if the condition is true then the break statement is executed and it terminate or jump the program.

PHP break statement example: 


Now consider an example using PHP. In this example we use for loop but you can use break statement in any kind of loop such as while loop, do-while loop etc.

<?php

for($count=0; $count<=10; $count++)

{

if($count==5)

break;

echo $count;

}

?>
Save the code, run it and try to understand what will be the output. This program show 01234 and when it founds 5 then it jump from the loop.

Comments

Popular posts from this blog

How to construct a B+ tree with example

How to show only month and year fields in android Date-picker?

Visitor Counter Script Using PHP