Anyone ever use a null switch for data validation?

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
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Anyone ever use a null switch for data validation?

Post by Benjamin »

Something like...

Code: Select all

switch (null) {
  default:

  if ($data == 'invalid') {
    $ErrorMessage = 'The Data was invalid';
    break;
  }

  // continue validation...

  // everything is ok...

  // insert data into db or something..
}  // end of switch
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's the use? I don't see it being helpful..
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

What is this for... goto emulation?
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Flow control, for example you have 20 fields to validate, but you want it to stop validation on the first error.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

astions wrote:Flow control, for example you have 20 fields to validate, but you want it to stop validation on the first error.
in that case lose the "break;"
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Well it's sudo code. In this example $Data was invalid, so it populated the error message and stopped execution.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

in that case lose the "break;"
Here break acts as an emergency exit lever.

I can see some uses for this technique, yet I would use do{ }while(false); instead:

Code: Select all

do {
  // do something
   if($condition)
       break; // escape
  // do something
} while(false);
Btw, it's documented in PHP manual as well: http://us2.php.net/manual/en/control-st ... .while.php
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

I wasn't aware of that, but I would hesitate to use a do while for something that I only want to execute once.

I really like the null switch method, but then again I thought of it myself. Not to say that no one has ever done it before, but I have never seen it done anywhere else at least.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

I wasn't aware of that, but I would hesitate to use a do while for something that I only want to execute once.
It's only a matter of habit. Once you know that switch is a special type of loop you don't see any substantial differences between your method and mine :)
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

Helpful. I have a question, though.

I very basically have this:

Code: Select all

function err($msg) {
  print("error: {$msg}");
}
function dosomething() {
  if (fail) {
    err("message");
    return false;
  }
  return true;
}

switch(null) {
  default:
  if (!dosomething()) break;
  if (!dosomethingelse()) break;
  if (!dosomething3()) break;
}
Is there a way I could do something like this...?

Code: Select all

function err($msg) {
  print("error: {$msg}");
}
function dosomething() {
  if (fail) {
    err("message");
    //break switch here
  }
}

switch(null) {
  default:
  dosomething();
  dosomethingelse();
  dosomething3();
}
It would come in handy with:

Code: Select all

foreach (db_fetch_row()) {
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post by Benjamin »

Not sure, it would probably work though. Might want to have a look at...

http://us2.php.net/manual/en/control-st ... .break.php
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Post by Skara »

No, just using break isn't going to work. I think the only solution is to throw an exception.
Post Reply