Bug #77585 [Csd->Nab]: in_array wrong answer
| From: | requinix@php.net | Date: | Fri, 08 Feb 2019 18:11:30 +0000 |
| Subject: | Bug #77585 [Csd->Nab]: in_array wrong answer | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-219447@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77585&edit=1
ID: 77585
Updated by: requinix@php.net
Reported by: giolaza at gmail dot com
Summary: in_array wrong answer
-Status: Closed
+Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: ubuntu
PHP Version: 7.2Git-2019-02-08 (Git)
Block user comment: N
Private report: N
Previous Comments:
------------------------------------------------------------------------
[2019-02-08 09:34:45] giolaza at gmail dot com
Thanks for answer
------------------------------------------------------------------------
[2019-02-08 09:33:36] sjon at hortensius dot net
You should checkout https://secure.php.net/manual/en/types.comparisons.php
- it shows 0 == "anything"
If you want strict checking, pass the $strict=true parameter to in_array
------------------------------------------------------------------------
[2019-02-08 09:32:20] spam2 at rhsoft dot net
http://php.net/manual/en/language.types.type-juggling.php
in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] ) : bool
well, in_array without the strict-param was exactly because of such behavior in the last security
chistmas calendar
<?php
$array=['users','documents','timePunch','vacation','requests'];
$var=0;
if(in_array($var,$array,TRUE)){
echo 'in_array IS TRUE';
}
else{
echo 'in_array IS FALSE';
}
echo PHP_EOL;
var_dump(in_array($var,$array,TRUE));
?>
------------------------------------------------------------------------
[2019-02-08 08:22:21] giolaza at gmail dot com
maybe. this result is because php tries to make same types during checking
if(intval($array[0])==$var)
because if(0=='string') is true too(
but I thing this is wrong;
------------------------------------------------------------------------
[2019-02-08 08:12:12] giolaza at gmail dot com
Description:
------------
in array return true if I am checking 0 var
if $var=0; result is wrong
int(0)
in_array IS TRUE
bool(true)
$var=(int) 1-1; result is wrong;
int(0)
in_array IS TRUE
bool(true)
if $var=1; result is current
int(1)
in_array IS FALSE
bool(false)
if $var=NULL; result is current
NULL
in_array IS FALSE
bool(false)
if $var=''; result is current
string(0) ""
in_array IS FALSE
bool(false)
Test script:
---------------
<?php
$array=['users','documents','timePunch','vacation','requests'];
$var=0;
if(in_array($var,$array)){
echo 'in_array IS TRUE';
}
else{
echo 'in_array IS FALSE';
}
echo PHP_EOL;
var_dump(in_array($var,$array));
Expected result:
----------------
in_array IS FALSE
bool(false)
Actual result:
--------------
in_array IS TRUE
bool(true)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77585&edit=1