Doc #67038 [Com]: in_array returns null
Edit report at https://bugs.php.net/bug.php?id=67038&edit=1
ID: 67038
Comment by: kilbyc at bellsouth dot net
Reported by: kilbyc at bellsouth dot net
Summary: in_array returns null
Status: Open
Type: Documentation Problem
Package: Documentation problem
Operating System: Windows 7
PHP Version: 5.5.11
Block user comment: N
Private report: N
New Comment:
Still a problem with consistency in the docs.
http://www.php.net/manual/en/function.fopen.php
lists the warning and undefined behavior.
Previous Comments:
------------------------------------------------------------------------
[2014-04-07 16:37:09] mfonda@php.net
Built-in PHP functions will generally return null if an invalid argument is passed. This is
documented at http://us1.php.net/manual/en/functions.internal.php:
"Note: If the parameters given to a function are not what it expects, such as passing an array
where a string is expected, the return value of the function is undefined. In this case it will
likely return NULL but this is just a convention, and cannot be relied upon."
------------------------------------------------------------------------
[2014-04-07 16:20:22] kilbyc at bellsouth dot net
It gets worse
What about the people who read the manual and see the bool sig, and therefore implement something
like:
$is_authorized = in_array('user',$roles)!==false;//and $roles failed to init
The developer doesn't know there is a potential warning there, or to look for a null.
------------------------------------------------------------------------
[2014-04-07 16:02:21] kilbyc at bellsouth dot net
Description:
------------
http://us1.php.net/in_array is at best misleading, at
worst, blatantly wrong.
The documentation clearly states that in_array returns bool, true, false. Null is not an acceptable
value in that list.
Also, the manual does not advise that this function may throw warnings.
This has been ignored for a while. https://bugs.php.net/bug.php?id=39788
Test script:
---------------
C:\php5.5>php --version
PHP 5.5.11 (cli) (built: Apr 2 2014 16:03:05)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
C:\php5.5>php -r "var_export(in_array('thing', null));"
Warning: in_array() expects parameter 2 to be array, null given in Command line
NULL
Expected result:
----------------
Update the manual to say that in_array issues Warning.
And:
Either returns NULL if the array is null.
OR
Fix the function to return false is the array is null.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=67038&edit=1
Thread (6 messages)