Req #72866 [Com]: in_array confusing strings and integers

From: Date: Wed, 17 Aug 2016 14:21:21 +0000
Subject: Req #72866 [Com]: in_array confusing strings and integers
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-203352@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=72866&edit=1

 ID:                 72866
 Comment by:         ryosuke_i_628 at yahoo dot co dot jp
 Reported by:        imbibinebe at gmail dot com
 Summary:            in_array confusing strings and integers
 Status:             Open
 Type:               Feature/Change Request
 Package:            *General Issues
 Operating System:   debian
 PHP Version:        5.6Git-2016-08-17 (Git)
 Block user comment: N
 Private report:     N

 New Comment:

This is VERY WELL-KNOWN feature of PHP, not a bug.
Just pass true as 3rd argument. (default is false)

Read documentation: http://php.net/manual/en/function.in-array.php

<?php

$needle = 's';
$elements = [1];

var_dump(in_array($needle, $elements, false)); // 's' == 1 -> bool(true)
var_dump(in_array($needle, $elements, true)); // 's' === 1 -> bool(false)


Previous Comments:
------------------------------------------------------------------------
[2016-08-17 14:02:53] imbibinebe at gmail dot com

Description:
------------
using PHP Version 5.6.14-0+deb8u1 on a debian operating system, here is what I get with the
following testing script.
I think that 'Expected result' should be obtained without using the third parameter of
in_array ([, bool $strict = FALSE ]) otherwise it is very confusing.

Test script:
---------------
echo 'test1:'.(in_array('string',[0,1])?'true':'false');
echo 'test2:'.(in_array('string',[1])?'true':'false');
echo 'test3:'.(in_array('string',[0])?'true':'false');

Expected result:
----------------
test1:false
test2:false
test3:false

Actual result:
--------------
test1:true
test2:false
test3:true


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



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


Thread (9 messages)

« previous php.bugs (#203352) next »