Page 1 of 1

Tons of {}.. better way?

Posted: Sun Jul 02, 2006 7:01 pm
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;
  //...
}

Posted: Sun Jul 02, 2006 7:10 pm
by Benjamin
Here are two ways you can do it.

viewtopic.php?t=50482

Posted: Sun Jul 02, 2006 7:15 pm
by Skara
Alright, cool. Thanks. I'd throw exceptions except I already have an error handler... >_>

This works. ;)