php.net |  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
Votes:2
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: 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: aaron dot hawley at uvm dot edu
New email:
PHP Version: OS:

 

 [2004-04-26 04:54 UTC] aaron dot hawley at uvm dot edu
Description:
------------
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:
---------------
<?php
error_reporting(E_ALL);
$not_an_array_yet[1] = 'a' . "\n";
exit("done\n");
?>

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

done

Actual result:
--------------
done

Patches

Pull Requests

History

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] iliaa@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

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:

http://www.php.net/manual/en/language.types.array.php

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

<?php
error_reporting(E_ALL); // This actually isn't needed.
array_push($not_an_array_yet, 'b');
exit("done\n");
?>

The above will output:

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

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

<?php
error_reporting(E_ALL);
$not_an_array_yet[] = 'b';
exit("done\n");
?>

The above will output:

done
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<http://www.php.net/array_push> 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-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 03:01:28 2024 UTC