Re: Switch statement Question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Boyd, Todd M. schreef:
>> -----Original Message-----
>> From: Alice Wei [mailto:ajwei@xxxxxxxxxxxxx]
>> Sent: Thursday, January 29, 2009 3:02 PM
>> To: php-general@xxxxxxxxxxxxx
>> Subject:  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;
> }

some people really don't like this kind of thing (hi Robbert :-)),
either way beware that the equality test is not strict, that is
to say autocasting occurs (variable type doesn't have to match)

an example:

switch (true) {
	case 1:
		echo "did you expect this?\n"; // <-- this is output
		break;
	case true:
		echo "or this?\n";
		break;
}



> ...this way, your case statements can be expressions themselves, and it
> will always pick at least one of them to fire.
> 
> HTH,
> 
> 
> // Todd
> 


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux