|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-10-18 18:46 UTC] lenar at city dot ee
Description:
------------
function in_array() returns wrong result.
No way (string) "test" is equal to (int) 0.
It is not right to cast needle to the type of array value.
The same problem manifests itself with array_search().
You will say propably that you've added third parameter to in_array() to take care of this, but in reality this isn't solution. The problem is you can't blindly cast needle.
And manual says: If needle is a string, the comparison is done in a case-sensitive manner.
Now tell me how you can compare a string you cast to (int) in case-sensitive manner?
And this is all bad because I discovered this by accident after a customer complained their stuff didn't work anymore.
That code used to work. I can swear. So you have broken BC it seems.
Reproduce code:
---------------
var_dump(in_array("test", array(0))
Expected result:
----------------
bool(false)
Actual result:
--------------
bool(true)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 21:00:01 2025 UTC |
I don't know then. All I'm sure the logic behind string and int comparison isn't as one would expect from language doing automatic type conversions. You should cast (string) to (int) or (float) only if (string) is numeric. Otherwise it's only logical to cast (int)/(float) operand to string before comparing. That's how I would expect automatic type conversion behave. Currently it's not intuitive. ("test" == 0) evaluating to true just doesn't seem right. Just my $0.02 (which I do not have).So, $array_symbols = array(0, '1', '5', 'abc', '<', 'hello'); print in_array('z', $array_symbols); Result: 1 does it normal?