|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #72817 array null index silently casts to empty string
Submitted: 2016-08-11 20:39 UTC Modified: 2017-01-12 11:04 UTC
Avg. Score:3.3 ± 1.7
Reproduced:1 of 2 (50.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: php at abiusx dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 7.0.9 OS: OS X 10.11
Private report: No CVE-ID: None
 [2016-08-11 20:39 UTC] php at abiusx dot com
Accessing an array will a null index is equal to accessing it with an empty string index, which is non-trivial and cause of many errors in applications.

When a developer writes 


They typically expect $index to be an index (regardless of type) and not null. But if it is null, the command still works and is equal to:

$a[""]=5; //same as $a[null]=5;

There is no trivial reason why null should cast to empty string, rather than say 0 or any other equivalent form. And this behavior typically should cause warnings or notices (although it breaks BC).

Test script:

Expected result:
E_NOTICE, E_WARNING or $a[0]=5, but not $a[""]=5

Actual result:
array(4) {


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2016-08-11 20:42 UTC] php at abiusx dot com
$a[false]=5 is equal to $a[0]=5
$a[true]=5 is equal to $a[1]=5

so why $a[null]=5 is $a[""]=5? Why do we even allow empty string indexes in an array!?
 [2017-01-12 11:04 UTC]
-Status: Open +Status: Not a bug
 [2017-01-12 11:04 UTC]
The docs actually mention this behavior so I am going to go with "not a bug".

> Null will be cast to the empty string, i.e. the key null will actually be stored under "".

Not saying I particularly like it. 

If anything it should be a feature request.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 17:01:31 2024 UTC