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
Re: switch with case 0 [message #182612 is a reply to message #182606] Sat, 17 August 2013 19:55 Go to previous messageGo to previous message
Thomas Mlynarczyk is currently offline  Thomas Mlynarczyk
Messages: 131
Registered: September 2010
Karma:
Senior Member
Norman Peelman schrieb:

> That's why it says "foo" = 0. Now is "foo" really 0, no - but it sure
> isn't greater than 0.

In order to compare "foo" and 0 one would need either to convert the
number 0 to a string ("0" would be the most reasonable result) or
convert "foo" to a number (not reasonably possible). PHP, unfortunately,
chooses the unreasonable alternative.

> PHP looks at the string in question one character
> at a time and builds a number until a non-numeric character is reached

I am aware of all that. It was decided to make PHP work that way. I was
merely pointing out that I consider it a very unfortunate decision to
cast strings to numbers instead of the other way round.

>>>> 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");
>>>> }
>>
>>> Sounds like you are trusting user input
>>
>> No he isn't, as the above code clearly shows. It would work perfectly
>> well in any normal programming language (although, personally, I would
>> have used in_array() instead of switch).
>>
>
> You are assuming $mode is never a string for one.

No I'm not assuming that. If all the SORT_* constants are integers, then
only the default branch should match in that case. It doesn't in PHP,
because PHP is made to implicitly convert strings to integers.

> For two, type
> juggling is done at the CASE statement, not the SWITCH statement.

I am aware of the fact that PHP will try to convert the string into a
number (even when this would not possibly make any sense). I'm just
saying that PHP should not have been designed that way.

Greetings,
Thomas


--
Ce n'est pas parce qu'ils sont nombreux à avoir tort qu'ils ont raison!
(Coluche)
[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 14:25:03 GMT 2024

Total time taken to generate the page: 0.04510 seconds