#49057 [Bgs]: The in_array Function
| From: | rasmus@php.net | Date: | Sat, 25 Jul 2009 17:58:01 +0000 |
| Subject: | #49057 [Bgs]: The in_array Function | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-140241@lists.php.net to get a copy of this message | ||
ID: 49057
Updated by: rasmus@php.net
Reported By: admin at djokodonev dot com
Status: Bogus
Bug Type: Unknown/Other Function
Operating System: Windows XP
PHP Version: 5.2.10
New Comment:
That's what the strict mode argument is for. Without it you have ==
comparisons and with it you have strict === checks. And since you are
checking a string against an array of integers the == check casts your
string to an integer. 'on'==0 which is in the array. Change your code
to use if(in_array($status, $valids, true)) and you will get your
desired result.
Previous Comments:
------------------------------------------------------------------------
[2009-07-25 16:46:05] jani@php.net
Sorry, but your problem does not imply a bug in PHP itself. For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system
is not the
appropriate forum for asking support questions. Due to the volume
of reports we can not explain in detail here why your report is not
a bug. The support channels will be able to provide an explanation
for you.
Thank you for your interest in PHP.
------------------------------------------------------------------------
[2009-07-25 16:18:50] admin at djokodonev dot com
Description:
------------
/*
This function should be changed...
When i check if a value is in array I would expect it to behave like
the equal $var1 == $var2, or $var1 === $var2 to check the types if they
are equal in the strict mode..
The in_array function should be changed if you ask me...
*/
$status = "on";
function is_valid(&$status) {
$valids = array(0, 1);
if (in_array($status, $valids)) {
return true;
}
else if ( $status == "on") {
$status = 1;
return true;
}
return false;
}
var_dump(is_valid($status));// => true;
var_dump($status); // => it is a striung "on"; Meaning it was validated
from the first part of the function where somehow it is found in the
array with valid stings(int) ...
/*
this returns true...
I guess i didn't expect it.. Maybe this is the way it should be?
When you use strings in the array ( => $valids = array("0", "1"); it
returns the expected results.
One of the notes on your page:
Note: If needle is a string, the comparison is done in a
case-sensitive manner. I don't understand how this can be used for any
purpose but to foll developers in getting wrong results from in_array
check..
Regards
*/
Reproduce code:
---------------
$status = "on";
function is_valid(&$status) {
$valids = array(0, 1);
if (in_array($status, $valids)) {
return true;
}
else if ( $status == "on") {
$status = 1;
return true;
}
return false;
}
var_dump(is_valid($status));// => true;
var_dump($status); // => it is a striung "on"; Meaning it was validated
from the first part of the function where somehow it is found in the
array with valid stings(int) ...
/*
this returns true...
I guess i didn't expect it.. Maybe this is the way it should be?
When you use strings in the array ( => $valids = array("0", "1"); it
returns the expected results.
Just thought to report it.
Regards
*/
Expected result:
----------------
The expected result is to get the string validated by the second
part... and get it converted to 1 based to the reference call..
I read the documentation...
And i see nowhere that if you use 0 or one in the array it returns
true...
This is illogical..
And very very confusing... and reasonless.
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=49057&edit=1