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 #182619 is a reply to message #182612] Sun, 18 August 2013 00:23 Go to previous messageGo to previous message
Norman Peelman is currently offline  Norman Peelman
Messages: 126
Registered: September 2010
Karma:
Senior Member
On 08/17/2013 03:55 PM, Thomas Mlynarczyk wrote:
> 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.
>

Why is it unreasonable? After all, FORM data is all strings to begin
with. PHP will type juggle "12345" to 12345 just fine. The whole idea
behind this after all is not about having strings like "2foo".

>> 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.
>

FORM data, GET/POST data, all strings...

>>>> > 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.
>

No, the only time the default is reached is when $mode is outside 0-7
(I say that because i only have 6 of the 8, I haven't updated yet.) If
$mode = E_ERROR it still validates. This code does nothing to ensure
$mode is what it is supposed to be.

What about:
-- code --
$mode = setSortMode("SORT_NUMERIC");
echo $mode."\n";

function setSortMode($mode)
{
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":
return constant($mode);
break;
default:
return SORT_REGULAR;
}
}
-- code --

Technically speaking, the user should not be able to *choose* an
invalid mode but if they somehow did, I believe it should default to
some normal behavior (even if the result isn't what they wanted.) In
other words, default to SORT_REGULAR (pick your poison) until another
*valid* option is chosen.

>> 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).

How do you know when it wouldn't make sense?

> I'm just saying that PHP should not have been designed that way.
>

FORMS, GET/POST data = strings...

> Greetings,
> Thomas
>
>


--
Norman
Registered Linux user #461062
-Have you been to www.php.net yet?-
[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 13:46:34 GMT 2024

Total time taken to generate the page: 0.04256 seconds