Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Break an Outer Loop in PHP



If there are two nested loops, the break statement can be used −

break 2;

Below is a demonstration with the foreach loop −

foreach(...) {
   foreach(...) {
      if (my_var_1.name == my_var_2)
      break 2; //it breaks out of the outermost foreach loop
   }
}

For PHP version>=5.3, the below lines of code can be used −

foreach (...) {
   foreach (...) {
      if (my_var_1.name == my_var_2)
      goto top;
   }
}
top:
Updated on: 2020-04-06T06:59:59+05:30

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements