php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #72866 change default of $strict parameter of in_array()
Submitted: 2016-08-17 14:02 UTC Modified: 2017-08-05 04:59 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: imbibinebe at gmail dot com Assigned:
Status: Suspended Package: *General Issues
PHP Version: 5.6Git-2016-08-17 (Git) OS: debian
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: imbibinebe at gmail dot com
New email:
PHP Version: OS:

 

 [2016-08-17 14:02 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-17 14:21 UTC] ryosuke_i_628 at yahoo dot co dot jp
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)
 [2016-08-17 14:27 UTC] ryosuke_i_628 at yahoo dot co dot jp
Sorry, I was missing your important sentence:

> 'Expected result' should be obtained without using the third parameter of in_array

BTW, I think this behavior seems to be a little odd, too...

Probably you know the related issue; array_unique() was broken at PHP 5.2.9 due to use SORT_REGULAR by default, which feature is reverted at PHP 5.2.10 to use SORT_STRING.
 [2016-08-17 14:32 UTC] imbibinebe at gmail dot com
And at least, the final result resulting of my test is:

// 'string' == 0 -> bool(true)

should that be allright ? :)
 [2016-08-17 15:34 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Type: Feature/Change Request +Type: Bug -Assigned To: +Assigned To: cmb
 [2016-08-17 15:34 UTC] cmb@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

The manual states[1]:

| If you compare a number with a string or the comparison involves
| numerical strings, then each string is converted to a number and
| the comparison performed numerically.

And[2]:

| The value is given by the initial portion of the string. If the
| string starts with valid numeric data, this will be the value
| used. Otherwise, the value will be 0 (zero).

[1] <http://php.net/manual/en/language.operators.comparison.php>
[2] <http://php.net/manual/en/language.types.string.php#language.types.string.conversion>
 [2016-08-17 15:50 UTC] imbibinebe at gmail dot com
Sorry, Submitting this as a bug was my mistake. I wished it as a feature request.
I think that having "bool $strict = FALSE" by default is very confusing (should be TRUE).
At least the 2nd rule you gave me here is complex enough for not being the default behaviour in the case of in_array function, I guess ...
Thank you all for your answers anyway.
 [2016-08-17 16:05 UTC] cmb@php.net
-Summary: in_array confusing strings and integers +Summary: change default of $strict parameter of in_array() -Status: Not a bug +Status: Re-Opened -Type: Bug +Type: Feature/Change Request
 [2016-08-17 16:05 UTC] cmb@php.net
> I wished it as a feature request.

Actually, you had submitted it as feature request, but it sounded
more like a bug report to me.

> I think that having "bool $strict = FALSE" by default is very
> confusing (should be TRUE).

Okay, that might be possible without RFC, but at least some
discussion on internals@lists.php.net seems appropriate. Feel free
to write to the list.
 [2016-08-17 16:06 UTC] cmb@php.net
-Assigned To: cmb +Assigned To:
 [2016-08-17 19:06 UTC] ryosuke_i_628 at yahoo dot co dot jp
>> And at least, the final result resulting of my test is:

>> // 'string' == 0 -> bool(true)

>> should that be allright ? :)

Ooooops!!
String starts with non-numeric char is equivalent to zero.
My examples were completely wrong, sorry...

Needless to say this feature is very very confusing lol
 [2017-08-05 04:59 UTC] stas@php.net
-Status: Re-Opened +Status: Suspended
 [2017-08-05 04:59 UTC] stas@php.net
Thank you for your interest in PHP and for submitting a feature request. Please be aware that due to the magnitude of change this request requires, it would be necessary to discuss it on PHP Internals list (internals@lists.php.net) as an RFC. Please read the guide about creating RFCs here:
https://wiki.php.net/rfc/howto
If you haven't had experience with writing RFCs before, it is advised to seek guidance on the Internals list (http://php.net/mailing-lists.php) and/or solicit help from one of the experienced developers. 

Please to not consider this comment as a negative view on the merits of your proposal - every proposal which requires changes of certain magnitude, even the very successful and widely supported ones, must be done through the RFC process. This helps make the process predictable, transparent and accessible to all developers.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC