|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2008-04-24 16:46 UTC] mcarpent at zenwerx dot com
Description:
------------
Accessing a null value as an array does not produce an error or even a warning. It resolves to null and the script continues.
Reproduce code:
---------------
<?php
$foo = null;
echo "Foo: {$foo['bar']}";
$foo = array();
echo "Foo: {$foo['bar']}";
?>
Expected result:
----------------
Notice: Undefined index: bar in c:\test\test.php on line 4
Foo:
Notice: Undefined index: bar in c:\test\test.php on line 7
Foo:
Actual result:
--------------
Foo:
Notice: Undefined index: bar in c:\test\test.php on line 7
Foo:
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 21 12:00:02 2025 UTC |
The string example was only used for testing after I found this particular issue. Real code was more like this: function returnNull() { return NULL; } $foo = returnNull(); // Resolves to null, therefore always false if ($foo['SomeIndex'] == 1) { // Never reached } This was a programmer error since it should have been checked for failure (null), but was left uncaught until the code was changed so that the function, which should return an array, returned an empty array instead of null to signify failure. If this is behaviour by design, it would be helpful for the documentation about silently casting the object to null to be located elsewhere as well. I would have never considered looking under the string documentation for an array issue.