RE: [PHP] Switch statement Question
| From: | Boyd, Todd M. | Date: | Thu, 29 Jan 2009 21:36:17 +0000 |
| Subject: | RE: [PHP] Switch statement Question | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-287372@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Alice Wei [mailto:ajwei@alumni.iu.edu]
> Sent: Thursday, January 29, 2009 3:02 PM
> To: php-general@lists.php.net
> Subject: [PHP] Switch statement Question
>
>
> Hi,
>
> I have a code snippet here as in the following:
>
> //Switch statements between the four options
> switch($string) {
> case "":
> $string= "NOT book.author='All'";
> break;
> default:
> $string= $string . "AND NOT book.author='All'";
> break;
> }
> This code does work, but I am wondering if it is possible in the
> switch statement clauses for me to do something like case does not
> equal to a certain author name if I don't want $string with that
> content to be processed. or, do I always use default in this case?
It's a bit non-conventional, but the switch block can be used like so:
switch(true) {
case (x < y):
dosomething();
break;
case (y == 0):
dosomethingelse();
break;
default:
somethingelseentirely();
break;
}
...this way, your case statements can be expressions themselves, and it
will always pick at least one of them to fire.
HTH,
// Todd