|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #64194 Null variable doesn't give error when trying to use as array
Submitted: 2013-02-12 10:50 UTC Modified: 2015-05-12 21:08 UTC
Avg. Score:4.8 ± 0.4
Reproduced:5 of 5 (100.0%)
Same Version:1 (20.0%)
Same OS:0 (0.0%)
From: marius at kitara dot nl Assigned:
Status: Verified Package: Arrays related
PHP Version: 5.4.11 OS: FreeBSD 9.1
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2013-02-12 10:50 UTC] marius at kitara dot nl
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:
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.


Add a Patch

Pull Requests

Pull requests:

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2013-02-18 12:53 UTC] 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-18 13:02 UTC]
-Status: Open +Status: Verified
 [2013-02-18 13:02 UTC]
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 (, in the other case it just directly returns NULL (

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).
 [2015-05-12 21:08 UTC]
Related to bug #65484.
PHP Copyright © 2001-2015 The PHP Group
All rights reserved.
Last updated: Sun Nov 29 15:01:40 2015 UTC