|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28151 give notice when storing to an undefined array
Submitted: 2004-04-26 04:54 UTC Modified: 2004-04-26 21:32 UTC
Avg. Score:2.0 ± 1.0
Reproduced:1 of 2 (50.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: aaron dot hawley at uvm dot edu Assigned:
Status: Not a bug Package: Variables related
PHP Version: 4.3.4 OS: Debian GNU/Linux
Private report: No CVE-ID:
 [2004-04-26 04:54 UTC] aaron dot hawley at uvm dot edu
When the left-hand-side of an assignment expression is an array location using square-brackets, PHP should check that the array variable was previously defined, and give a notice if E_NOTICE is flagged in error_reporting.

This would find mistyped variables, and help with those attempting to write more stable and secure PHP code with E_ALL.

Reproduce code:
$not_an_array_yet[1] = 'a' . "\n";

Expected result:
Notice: Undefined variable:  not_an_array_yet in my_file.php on line 3


Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-26 04:56 UTC] aaron dot hawley at uvm dot edu
don't let the concatenated endline character confuse you.
sorry about that.
 [2004-04-26 19:30 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

Expected behaviour 
 [2004-04-26 21:32 UTC] aaron dot hawley at uvm dot edu
-Status:           Open
+Status:           Bogus

> Expected behaviour

Funny, I actually expected this response to my bug.  I think that's called recursion. [smiley-face]

I entertained your request of me to double-check the documentation.  I could only find the following mentions in the documentation:

"If $arr doesn't exist yet, it will be created. So this is also an alternative way to specify an array."

I couldn't find anything about "error reporting" for this situation (is this a [small] documentation bug?).  I'm not requesting the automatic-array-creation behavior be changed, I'm requesting a "Notice"--not even a "Warning" (see below)--be generated when E_NOTICE is set. 

Continuing in the documentation, there were numerous examples and array-"Do's and Don't's" showing which array index expresions generate E_NOTICE errors.  But nothing was mentioned of storing to indexes of previously undefined variables (I couldn't find documention on the "undefined variable" notice, either).

For the above, I only bothered looking closely at the following section of the manual:

I'm sure I would have had more success had I made a more general, thorough and thus convincing argument.  But instead I'll just try to show the inconsistency of this "Expected behavior" with the following examples:

Example 2-1: Undefined array gives warning

error_reporting(E_ALL); // This actually isn't needed.
array_push($not_an_array_yet, 'b');

The above will output:

Warning: array_push(): First argument should be an array in example_2-1.php on line 3

Example 2-2: PHP syntax of undefined array without warning

$not_an_array_yet[] = 'b';

The above will output:

Shouldn't it have been more "expected" for these to have behaved the same?  They did not.  And I'm actually just arguing for a "Notice" message and not the "Warning" which array_push() gives.  Because, as you may know, array_push doesn't create an array while the PHP syntax does.

So I submit these should have more comparable semantic definitions and thus both give warnings regardless if the PHP programmer chooses to use the more succinct PHP array syntax over using a function call. If there is some larger semantic difference between these two, I'm not aware of it.  Interestingly, the documentation suggests<> using the empty-bracket notation.

Perhaps I should have categorized this as a "feature request"?  Because it is useful for PHP to make sure your array was previously defined and not simply a mistyped variable.
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Tue Aug 29 15:01:52 2017 UTC