Bug #74509 [Opn->Nab]: array_shift($foo) throws "null given" instead of "Undefined variable"
| From: | requinix@php.net | Date: | Fri, 28 Apr 2017 16:55:03 +0000 |
| Subject: | Bug #74509 [Opn->Nab]: array_shift($foo) throws "null given" instead of "Undefined variable" | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-208842@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=74509&edit=1
ID: 74509
Updated by: requinix@php.net
Reported by: pov at fingerprint dot fr
Summary: array_shift($foo) throws "null given" instead of
"Undefined variable"
-Status: Open
+Status: Not a bug
Type: Bug
-Package: PHP Language Specification
+Package: Variables related
PHP Version: 5.6.30
Block user comment: N
Private report: N
New Comment:
All those functions have one thing in common: the argument is by reference. PHP does not issue
warnings when creating references to variables that do not exist, which is why code like
function add($a, $b, &$c) {
$c = $a + $b;
}
add(1, 2, $sum);
works without problems.
Changing that as a whole would create many warnings out of nowhere for use cases that were easily
justifiable before. The alternative would be a number of tweaks throughout PHP to make (some)
functions that use references warn if the variable did not already exist, but given that the purpose
would be to turn a clear E_WARNING into the less severe (and often ignored) E_NOTICE, I don't
think doing so is worth it.
Previous Comments:
------------------------------------------------------------------------
[2017-04-28 16:15:03] pov at fingerprint dot fr
Description:
------------
When applying array_shift on a non-existing variable, a warning about array_shift is emitted
(array_shift() expects parameter 1 to be array, null given) instead of a notice on the non-existing
variable.
Tested on all the following packaged versions :
7.0.17-2+deb.sury.org~trusty+1
7.1.3-2+deb.sury.org~trusty+1
5.5.38-4+deb.sury.org~trusty+1
5.6.30-7+deb.sury.org~trusty+1
-- Additional tests
This bug probably applies on other functions as well.
Some tests gives interesting results (I note NUV = Notice : Undefined variable - ok -, WNG = Warning
: null given - which shouldn't be) :
array_shift($foo) WNG
array_slice($foo) NUV
array_unshift($foo, 1) WNG
array_unshift($foo, $bar) NUV bar (logically, should be NUV $foo first)
array_pop($foo) WNG
array_sum($foo) NUV
empty($foo) returns true
htmlentities($foo) NUV
Test script:
---------------
error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', true);
echo '$foo = '; echo $foo; echo "\n";
echo 'array_shift($foo) = '; echo array_shift($foo); echo "\n";
exit;
Expected result:
----------------
$foo =
Notice: Undefined variable: foo in index.php on line 4
array_shift($foo) =
Notice: Undefined variable: foo in index.php on line 4
Actual result:
--------------
$foo =
Notice: Undefined variable: foo in index.php on line 4
array_shift($foo) =
Warning: array_shift() expects parameter 1 to be array, null given in index.php on line 5
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74509&edit=1