php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48121 Reference inconsistency
Submitted: 2009-04-30 13:51 UTC Modified: 2009-04-30 15:38 UTC
From: feldkamp at gameforge dot de Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.9 OS: Debian Etch 2.6.26
Private report: No CVE-ID: None
 [2009-04-30 13:51 UTC] feldkamp at gameforge dot de
Description:
------------
Using array-operator to access a non-existing key of a referenced variable turns variables that are false or an empty string into arrays.

Reproduce code:
---------------
<?
$abc = false; // or ''
var_dump($abc);
$a = &$abc['unsetKey'];
var_dump($abc);
?>

Expected result:
----------------
Warning: Cannot use a scalar value as an array

Actual result:
--------------
boolean false

array
  'unsetKey' => null

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-04-30 15:16 UTC] jani@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

$a =& $foo[]; 

Where =& means that both $a and $foo[<next index>] should point to 
same place, and in your example they both point to null so:

var_dump($a, $foo);

Outputs:

NULL
array(1) {
  [0]=>
  &NULL
}


For more info:
http://www.php.net/manual/en/language.references.whatare.php

 [2009-04-30 15:38 UTC] feldkamp at gameforge dot de
In the example you bring up, the variables $a and $foo are not set (null) => behaviour is as expected and as documented.

BUT: If you have the variable initialized (for example with false or an empty string) your behaviour also come up (which it shouldn't in my humble opinion)

if you use (boolean) true or a non-empty string, you get:
"Warning: Cannot use a scalar value as an array"

if you use (boolean) false or an empty string, you get:

boolean false
array
  'unsetKey' => null
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 24 00:00:03 2025 UTC