Jump to content

Break out of multiple loops such as goto?

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Fighter

Fighter

    Newbie

  • Members
  • PipPip
  • 28 posts
In this situation of looping infinitely and parsing a lines in a file, I am required to break out of a few loops while being in the script, so exit is not an option. I have looked in the documentation and goto is there, although unfortunately? it seems to be for a newer PHP version than I have.

How would I break out of something like this, assuming it really is much more complex:
for($i = 0; $i <= 900; $i++) {

   foreach($second as $something) {

       switch($something) {

           case 'a' : $last = 'foo'; break;

           case 'b' : $last = 'bar'; break;

           case '...': break;break;somehow-break;

       }

   }
}

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
One thing you may not be aware of, is that PHP addresses this limitation by adding break level modifier in their language construct.

The following should work:
for($i = 0; $i <= 900; $i++) { 
   foreach($second as $something) { 
       switch($something) { 
           case 'a' : $last = 'foo'; break; 
           case 'b' : $last = 'bar'; break; 
           case '...': break 3;
       } 
   }

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users