FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » switch with case 0
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
switch with case 0 [message #182589] Sat, 17 August 2013 04:55 Go to previous message
Fiver is currently offline  Fiver
Messages: 35
Registered: July 2013
Karma:
Member
This example

switch ("foo") {
case 0:
echo "'foo' is 0\n";
break;
default:
echo "'foo' is not 0\n";
}

will print: 'foo' is 0.

I know how to avoid the problem, I'm just wondering what kind of
comparison is used that makes "foo" equal to 0? The manual mentions
"loose comparison", but this is even looser than usual.

By the way, the original code was meant to validate SORT_* arguments.
It's a little harder to spot here:

switch ($mode) {
case SORT_ASC:
case SORT_DESC:
case SORT_REGULAR:
case SORT_NUMERIC:
case SORT_STRING:
case SORT_LOCALE_STRING:
case SORT_NATURAL:
case SORT_FLAG_CASE:
break;
default:
throw new \InvalidArgumentException("invalid sort mode: $mode");
}

SORT_REGULAR is 0, so if the input is a string, this will break (literally).


regards,
5er
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: AND and OR
Next Topic: Compiling PHP 5.x on Cygwin
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 11:39:31 GMT 2024

Total time taken to generate the page: 0.04877 seconds