Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old March 7th, 2006, 05:25 PM
bobmct
Guest
 
Posts: n/a
Default PHP equiv of perl's last label or goto?

In a multiple nested structure I have a section that when true need to
break out of not only the current structure, but several more to read
a much higher level. In perl I would name/label my structures and use
the "last labelname" statement to accomplish this.

The PHP docs state that its break statement can take an optional
parameter which represents the "number" of levels to break out of.
Now this seems like it could work. However, I see a problem with
ongoing maintenance if someone inserts or removes another level which
would cause that number to be inaccurate.

Can someone please recommend how one might accomplish this feat using
flexible php code? I am all ears (well, eyes).

Thanks,

Bob
  #2  
Old March 7th, 2006, 06:55 PM
NC
Guest
 
Posts: n/a
Default Re: PHP equiv of perl's last label or goto?

bobmct wrote:[color=blue]
>
> In a multiple nested structure I have a section that when true need to
> break out of not only the current structure, but several more to read
> a much higher level. In perl I would name/label my structures and use
> the "last labelname" statement to accomplish this.
>
> The PHP docs state that its break statement can take an optional
> parameter which represents the "number" of levels to break out of.
> Now this seems like it could work. However, I see a problem with
> ongoing maintenance if someone inserts or removes another level which
> would cause that number to be inaccurate.
>
> Can someone please recommend how one might accomplish this feat
> using flexible php code? I am all ears (well, eyes).[/color]

Put the code into a function and break out using return().
Alternatively, use a flag variable:

$flag = true;
while ($flag) {
// some PHP code...
if ($some_condition) {
$flag = false;
}
// more PHP code...
if ($another_condition) {
$flag = false;
continue;
}
// yet more PHP code...
}

Cheers,
NC

  #3  
Old March 8th, 2006, 09:15 AM
Toby Inkster
Guest
 
Posts: n/a
Default Re: PHP equiv of perl's last label or goto?

bobmct wrote:
[color=blue]
> The PHP docs state that its break statement can take an optional
> parameter which represents the "number" of levels to break out of.
> Now this seems like it could work. However, I see a problem with
> ongoing maintenance if someone inserts or removes another level which
> would cause that number to be inaccurate.[/color]

The number of levels can be a variable rather than hard-coded, so there
might be scope to do something there, but it might be a bit nasty.

PHP's lack of "next" and "last" are a little irritating.

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact

 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles