Bug #64194 [Ver]: Null variable doesn't give error when trying to use as array

From: Date: Tue, 12 May 2015 21:08:46 +0000
Subject: Bug #64194 [Ver]: Null variable doesn't give error when trying to use as array
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-192646@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=64194&edit=1

 ID:                 64194
 Updated by:         cmb@php.net
 Reported by:        marius at kitara dot nl
 Summary:            Null variable doesn't give error when trying to use
                     as array
 Status:             Verified
 Type:               Bug
 Package:            Arrays related
 Operating System:   FreeBSD 9.1
 PHP Version:        5.4.11
 Block user comment: N
 Private report:     N

 New Comment:

Related to bug #65484.


Previous Comments:
------------------------------------------------------------------------
[2013-02-18 13:02:01] nikic@php.net

I would tentatively classify this as a bug. The fact that NULL can be cast to an array by setting a
key (without notice) is intentional, but accessing an undefined key on NULL should throw a notice.
Same applies to other scalar like ints/doubles/...

This is a behavioral difference between zend_fetch_dimension_address and
zend_fetch_dimension_address_read. In one case an array will be created and dispatched to
fetch_from_array (http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_execute.c#1119), in the other case it
just directly returns NULL (http://lxr.php.net/xref/PHP_TRUNK/Zend/zend_execute.c#1265).

This issue also creates a behavioral difference between BP_VAR_R fetches and BP_VAR_RW. E.g.
++$data['wok'] in the above example *will* throw a notice, even though it should be the
same as doing $data['wok'] = $data['wok'] + 1 (which does not throw a notice).

------------------------------------------------------------------------
[2013-02-18 12:53:19] kevin dot swinton at gmail dot com

Without diving into the internals at this point:

$data is initially declared as a variable of type NULL.

In the first echo statement, the attempt to reference an index of 'wok' in $data is
effectively ignored: the variable is not an ARRAY to begin with.

$data is then cast into a variable of type ARRAY ("$data['test'] =
'test';")

In the second echo statement, $data is an array, with an index, which PHP can enumerate against and
ultimately determine that index 'wok' is not present.

Hence the first echo results in a NULL output, whilst the second results in PHP issuing an E_NOTICE.

I'd submit this issue is not related to the PHP core.

------------------------------------------------------------------------
[2013-02-12 10:50:42] marius at kitara dot nl

Description:
------------
When you define a string as null value you won't get any undefined errors. As soon you define a
value in that same string you will see undefined errors.

Test script:
---------------
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL | E_STRICT);

$data = null;
echo $data['wok']; // Remains silent
$data['test'] = 'test';
echo $data['wok']; // Gives an undefined error


Expected result:
----------------
Both echo lines should give an undefined error.



------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=64194&edit=1


Thread (5 messages)

« previous php.bugs (#192646) next »