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: 2018-07-13 15:22 UTC
Votes:7
Avg. Score:4.6 ± 0.7
Reproduced:6 of 7 (85.7%)
Same Version:1 (16.7%)
Same OS:0 (0.0%)
From: marius at kitara dot nl Assigned: cmb (profile)
Status: Duplicate Package: Arrays related
PHP Version: 5.4.11 OS: FreeBSD 9.1
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: marius at kitara dot nl
New email:
PHP Version: OS:

 

 [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

Pull Requests

Pull requests:

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).
 [2015-05-12 21:08 UTC] cmb@php.net
Related to bug #65484.
 [2018-07-13 15:22 UTC] cmb@php.net
-Status: Verified +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2018-07-13 15:22 UTC] cmb@php.net
Duplicate of bug #54556.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 17:01:32 2024 UTC