CopyPastehas never been so tasty!

Break in nested loops in PHP

by anonymous

  • 0
  • 0
  • 0
105 views

Orignal Source:http://codingvilla.com/break-nested-loops-php-single-article-600.aspx

 

Code:

 

<?php

 

for ($i=0;$i<5;$i++){

            for($j=0;$j<5;$j++){

                        if($j==3)

                        break;

                        echo "\$j &nbsp;&nbsp;=$j<br/>";

            }

}

?>

 

 

Output:

 

$j   =0
$j   =1
$j   =2
$j   =0
$j   =1
$j   =2
$j   =0
$j   =1
$j   =2
$j   =0
$j   =1
$j   =2
$j   =0
$j   =1
$j   =2

 

 

Explanation:

 

In the above example we have two for loops $i=0 to 5, $j=0 to $j=5. When $j==3 we break the loop as you see in the output.

Add A Comment: