Edit report at http://bugs.php.net/bug.php?id=30473&edit=1

 ID:                 30473
 Comment by:         alpust at gmail dot com
 Reported by:        lenar at city dot ee
 Summary:            in_array not working correctly
 Status:             Open
 Type:               Feature/Change Request
 Package:            Feature/Change Request
 Operating System:   Debian
 PHP Version:        4.3.9
 Block user comment: N

 New Comment:

So,



$array_symbols = array(0, '1', '5', 'abc', '<', 'hello');

print in_array('z', $array_symbols);



Result: 1



does it normal?


Previous Comments:
------------------------------------------------------------------------
[2004-10-19 13:38:18] lenar at city dot ee

Trying this as feature request to get review...

------------------------------------------------------------------------
[2004-10-19 13:36:14] lenar at city dot ee

One more comment.



What I want to say is that comparisons in array functions should happen
like 'eq' operator does it in perl.



That would be intuitive. You can't change this, I presume, but that
would be the correct way IMHO.

------------------------------------------------------------------------
[2004-10-19 11:56:05] lenar at city dot ee

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

------------------------------------------------------------------------
[2004-10-19 09:13:42] der...@php.net

Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

There is no BC here, I tried a couple of versions (4.0.0, 4.0.7, 4.1.2,
4.2.3, 4.3.5-4.3.9) and all do this:



der...@tequila:/dat/dev/php/php-4.0.0$ ./php

<?php

var_dump(in_array("test", array(0)));

?>

X-Powered-By: PHP/4.0.0

Content-type: text/html



bool(true)





------------------------------------------------------------------------
[2004-10-18 18:46:24] 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)


------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=30473&edit=1

Reply via email to