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
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Wed Jun 21, 2006 12:12 pm
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Wed Jun 21, 2006 12:15 pm
what's the use? I don't see it being helpful..
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Wed Jun 21, 2006 12:15 pm
What is this for... goto emulation?
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Wed Jun 21, 2006 12:21 pm
Flow control, for example you have 20 fields to validate, but you want it to stop validation on the first error.
patrikG
DevNet Master
Posts: 4235 Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK
Post
by patrikG » Wed Jun 21, 2006 12:30 pm
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;"
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Wed Jun 21, 2006 12:34 pm
Well it's sudo code. In this example $Data was invalid, so it populated the error message and stopped execution.
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Wed Jun 21, 2006 12:59 pm
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
Benjamin
Site Administrator
Posts: 6935 Joined: Sun May 19, 2002 10:24 pm
Post
by Benjamin » Wed Jun 21, 2006 1:03 pm
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.
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Wed Jun 21, 2006 1:12 pm
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
Skara
Forum Regular
Posts: 703 Joined: Sat Mar 12, 2005 7:13 pm
Location: US
Post
by Skara » Sun Jul 02, 2006 7:24 pm
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:
Skara
Forum Regular
Posts: 703 Joined: Sat Mar 12, 2005 7:13 pm
Location: US
Post
by Skara » Mon Jul 03, 2006 1:53 pm
No, just using break isn't going to work. I think the only solution is to throw an exception.