php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #30473 in_array not working correctly
Submitted: 2004-10-18 18:46 UTC Modified: 2010-08-12 14:37 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: lenar at city dot ee Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 4.3.9 OS: Debian
Private report: No CVE-ID: None
 [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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-19 09:13 UTC] derick@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:

derick@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-19 11:56 UTC] 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 13:36 UTC] 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 13:38 UTC] lenar at city dot ee
Trying this as feature request to get review...
 [2010-08-12 14:27 UTC] alpust at gmail dot com
So,

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

Result: 1

does it normal?
 [2010-08-12 14:37 UTC] johannes@php.net
-Status: Open +Status: Bogus -Package: Feature/Change Request +Package: *General Issues
 [2010-08-12 14:37 UTC] johannes@php.net
in_array has a third parameter to be strict since PHP 4.2.0.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Sep 18 22:01:26 2024 UTC