Home  >  Article  >  Backend Development  >  How to break outer loop using PHP?

How to break outer loop using PHP?

PHPz
PHPzforward
2023-08-29 15:21:03978browse

How to break outer loop using PHP?

If there are two nested loops, you can use the break statement-

break 2;

The following is a demonstration of 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, you can use the following line of code -

foreach (...) {
   foreach (...) {
      if (my_var_1.name == my_var_2)
      goto top;
   }
}
top:

The above is the detailed content of How to break outer loop using PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete