Doc #44821 [Nab->Csd]: Accessing NULL as an Array does not produce error

From: Date: Fri, 30 Dec 2016 16:00:30 +0000
Subject: Doc #44821 [Nab->Csd]: Accessing NULL as an Array does not produce error
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-14300@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=44821&edit=1 ID: 44821 Updated by: cmb@php.net Reported by: mcarpent at zenwerx dot com Summary: Accessing NULL as an Array does not produce error -Status: Not a bug +Status: Closed Type: Documentation Problem -Package: Documentation problem +Package: Arrays related Operating System: Windows XP PHP Version: 5.2.5 -Assigned To: +Assigned To: cmb Block user comment: N Private report: N New Comment: This behavior has been documented in the meantime, see <http://svn.php.net/viewvc?view=revision&revision=339937>. Note that there's request #37676, which is about changing this behavior. Previous Comments: ------------------------------------------------------------------------ [2013-08-21 23:59:11] matteosistisette at gmail dot com Forgot to mention: NEEDS TO BE REOPENED ------------------------------------------------------------------------ [2013-08-21 23:58:02] matteosistisette at gmail dot com "It is properly documented: "Converting NULL to an array results in an empty"." THAT's WRONG!! And both the original report and the first comment already point this out. Accessing a non-existing key of an empty array produces an E_NOTICE, Hence, if you try to access a key of null, and null was converted to an empty array, you would be accessing an element of an empty array, and hence you should get an E_NOTICE. But you don't. This IS a bug. Or a documentation problem if there is a good reason (which I cannot see) for such an absurd behavior. It is definitely not as documented. Compare these: $arr=array(); echo $arr['lsjsdfjls']; // issues an E_NOTICE $arrb=null; echo $arrb['losjfbls']; // doesn't issue an E_NOTICE This made me waste a day debugging. ------------------------------------------------------------------------ [2008-11-05 17:02:26] vrana@php.net It is properly documented: "Converting NULL to an array results in an empty". ------------------------------------------------------------------------ [2008-04-24 17:14:35] mcarpent at zenwerx dot com 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. ------------------------------------------------------------------------ [2008-04-24 16:59:00] florian dot ember at gmail dot com http://php.net/language.types.string#language.types.string.substr "Note: Accessing variables of other types using [] or {} silently returns NULL." Typecasting only happens when you explicitly do it with (array). ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=44821 -- Edit this bug report at https://bugs.php.net/bug.php?id=44821&edit=1

« previous php.doc.bugs (#14300) next »