your image

PHP break - javatpoint

javapoint
Related Topic
:- PHP Java computer programming

PHP Break

PHP break statement breaks the execution of the current for, while, do-while, switch, and for-each loop. If you use break inside inner loop, it breaks the execution of inner loop only.

The break keyword immediately ends the execution of the loop or switch structure. It breaks the current flow of the program at the specified condition and program control resumes at the next statements outside the loop.

The break statement can be used in all types of loops such as while, do-while, for, foreach loop, and also with switch case.

Syntax

  1. jump statement;  
  2. break;  

Flowchart

PHP Break: inside loop

Let's see a simple example to break the execution of for loop if value of i is equal to 5.

 

Is php dead, and why should you care?

  1. <?php    
  2. for($i=1;$i<=10;$i++){    
  3. echo "$i <br/>";    
  4. if($i==5){    
  5. break;    
  6. }    
  7. }    
  8. ?>  

Output:

12345

PHP Break: inside inner loop

The PHP break statement breaks the execution of inner loop only.

  1. <?php    
  2. for($i=1;$i<=3;$i++){    
  3.  for($j=1;$j<=3;$j++){    
  4.   echo "$i   $j<br/>";    
  5.   if($i==2 && $j==2){    
  6.    break;    
  7.   }    
  8.  }    
  9. }    
  10. ?>  

Output:

1 11 21 32 12 23 13 23 3

PHP Break: inside switch statement

The PHP break statement breaks the flow of switch case also.

  1. <?php        
  2. $num=200;        
  3. switch($num){        
  4. case 100:        
  5. echo("number is equals to 100");        
  6. break;        
  7. case 200:        
  8. echo("number is equal to 200");        
  9. break;        
  10. case 50:        
  11. echo("number is equal to 300");        
  12. break;        
  13. default:        
  14. echo("number is not equal to 100, 200 or 500");        
  15. }       
  16. ?>  

Output:

number is equal to 200

PHP Break: with array of string

  1. <?php  
  2. //declare an array of string  
  3. $number = array ("One", "Two", "Three", "Stop", "Four");  
  4. foreach ($number as $element) {  
  5. if ($element == "Stop") {  
  6. break;  
  7. }  
  8. echo "$element </br>";  
  9. }  
  10. ?>  

Output:

OneTwoThree

You can see in the above output, after getting the specified condition true, break statement immediately ends the loop and control is came out from the loop.

PHP Break: switch statement without break

It is not essential to break out of all cases of a switch statement. But if you want that only one case to be executed, you have to use break statement.

 

  1. <?php  
  2. $car = 'Mercedes Benz';  
  3. switch ($car) {    
  4. default:  
  5. echo '$car is not Mercedes Benz<br>';  
  6. case 'Orange':  
  7. echo '$car is Mercedes Benz';  
  8. }  
  9. ?>  

Output:

$car is not Mercedes Benz$car is Mercedes Benz

PHP Break: using optional argument

The break accepts an optional numeric argument, which describes how many nested structures it will exit. The default value is 1, which immediately exits from the enclosing structure.

  1. <?php  
  2. $i = 0;  
  3. while (++$i) {  
  4.     switch ($i) {  
  5.         case 5:  
  6.             echo "At matched condition i = 5<br />\n";  
  7.             break 1;  // Exit only from the switch.   
  8.        case 10:  
  9.             echo "At matched condition i = 10; quitting<br />\n";  
  10.             break 2;  // Exit from the switch and the while.   
  11.        default:  
  12.             break;  
  13.     }  
  14. }?>  

Output:

At matched condition i = 5At matched condition i = 10; quitting

Note: The break keyword immediately ends the execution of the current structure.

Comments