php.net |  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: 2013-02-18 13:02 UTC
Votes:3
Avg. Score:4.7 ± 0.5
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.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
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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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] nikic@php.net
-Status: Open +Status: Verified
 [2013-02-18 13:02 UTC] 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).
 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 16 10:02:09 2014 UTC