Label If and loop condition strucutres

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Label If and loop condition strucutres

Post by kendall »

can you put a label to condition and loop statements so that you can break and continue them using thier label name identifier like what happens in javascript

Kendall
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

No you can't. But you may use break <level>; and continue <level>; constructs like this:

Code: Select all

foreach(range(1,10) as $elt) {
  foreach(range(1,10) as $innerElt) {
    echo "[$elt, $innerElt]";
    if($innerElt>5) 
       continue 2; // continue outer loop
  }
}
Last edited by Weirdan on Mon Nov 28, 2005 8:38 am, edited 1 time in total.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Re: Label If and loop condition strucutres

Post by Roja »

kendall wrote:can you put a label to condition and loop statements so that you can break and continue them using thier label name identifier like what happens in javascript

Kendall
What you are describing is called a "labeled break", and currently php doesn't have one. However, there are discussions about possibly adding it in php6.
Post Reply