Re: PHP is Zero
| From: | Stuart Dallas | Date: | Thu, 13 Jun 2013 12:20:10 +0000 |
| Subject: | Re: PHP is Zero | ||
| References: | 1 2 3 4 5 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-321389@lists.php.net to get a copy of this message | ||
On 13 Jun 2013, at 12:27, BUSCHKE Daniel <Daniel.BUSCHKE@nextiraone.eu> wrote:
> Hi,
>
>> It gives up when it finds a non-numeric character (as the documentation would tell you)
>
> Why is PHP doing that? I know it works as designed and I know it is documented like this but
> that does not mean that it is a good feature, does it? So lets talk about the question: Is that
> behaviour awaited by PHP software developers? Is that really the way PHP should work here? May we
> should change that?!
>
> BTW: I talked to some collegues and friends since my first post. They all guessed that
> "'PHP' == 0" is false within a few seconds. I think the weak-typed-PHP is a
> little to weak at this point.
I don't know what the actual reasoning is; if you want that I suggest you ask on the internals
list, but be careful how you phrase the question. If it's pure curiosity then that's fine,
but whether you agree with the reasoning or not it's gone way past the point where it can be
changed.
My take it that the behaviour is perfectly reasonable. When you compare two different types it has
to convert one of them. If you're comparing a string to a number it seems logical to me that
the number should be the preferred type, so the string gets converted. What would you expect
array(5) == 5 to give you? Would you expect the 5 to be converted in to an array? No. The reason it
looks odd for "PHP" == 0 is that there's no reason why the 0 should not be converted
to "0", except that strings and numbers are not the only types available and there has to
be an order of precedence. And consistency is key.
As for how strings get converted to numbers I have no idea why it will take everything up to the
first non-e|E|. character. The documentation[1] simply states the behaviour and doesn't go in
to why. However, I have to ask why it matters? Why would you want to convert
'8315e839da08e2a7afe6dd12ec58245d' to an integer? If you have code that's juggling it
automatically, be more explicit. It's fine to be interested in the reasons why it does what it
does, but if it's causing you problems then the issue is with your code not being explicit
enough, not PHP.
-Stuart
[1] http://php.net/language.types.string#language.types.string.conversion
--
Stuart Dallas
3ft9 Ltd
http://3ft9.com/