Tons of {}.. better way?

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
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Tons of {}.. better way?

Post by Skara »

Alright, say you have something like this:

Code: Select all

if ($a) {
  //stuff
  if ($b) {
    //stuff
    if ($c) {
    //stuff
      if ($d) {
        //finish
      }
    }
  }
}
and so on.
Might it be better to do something like this? (I don't how to do gotos in php, by the way.)

Code: Select all

if (!$a) goto quit;
//stuff
if (!$b) goto quit;
//stuff
if (!$c) goto quit;
//...
:quit
Not that I particularly like gotos...

Or is there an even better way? I ask because getting so many brackets becomes annoying.
I'm basically doing error checking, but I don't want to die() if I find an error--I just want to stop what I'm doing and go to the next thing.

Would something like this be possible? (Just came to me...)

Code: Select all

{
  if (!$a) break;
  //stuff
  if (!$b) break;
  //...
}
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Here are two ways you can do it.

viewtopic.php?t=50482
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Alright, cool. Thanks. I'd throw exceptions except I already have an error handler... >_>

This works. ;)
Post Reply