--- Eugene Lee <[EMAIL PROTECTED]> wrote: > : switch (true) { > : case ($var === 'TEST-1')?true:false: > : case ($var === 'TEST-2')?true:false: > : case ($var === 'TEST-2')?true:false: > : do something > : } > > Oh man, that's just sick...
Partially because it's unnecessarily complex. This is like saying: if ($var === 'TEST-1') { $expression = true; } else { $expression = false; } if ($expression) { ... While the ternary operator makes this redundancy less obvious, it only adds to the complexity and lack of readability. Consider the following code as a substitute for the above example: if ($var === 'TEST-1') { ... Hopefully that part is clear. Now, on to the original question. Try this example: <? $foo = 'bar'; switch (true) { case ($foo === 'notbar'): echo 'A'; break; case ($foo === 'bar'); echo 'B'; break; default: echo 'C'; break; } ?> This should output B. You will also notice that it "works" when you switch on $foo instead of the boolean true, but this is misleading. PHP converts $foo to the boolean true when comparing to the expressions, because we set it to the string bar. To understand this point further, try this example: <? $foo = 0; switch ($foo) { case ($foo === 'notbar'): echo 'A'; break; case ($foo === 0); echo 'B'; break; default: echo 'C'; break; } ?> This should also output B. That seems to be wrong, but in this case it is comparing each expression to $foo, which is the integer 0 in this case, so it evaluates to false. So, you will see A, because ($foo === 'notbar') also evaluates to false. Recap: 1. You can switch on anything, including boolean values. 2. Your cases can be expressions to be evaluated. Hope that helps. Chris ===== Become a better Web developer with the HTTP Developer's Handbook http://httphandbook.org/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php