php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #28503 Operator $x[$y] behavior is incorrect
Submitted: 2004-05-24 11:18 UTC Modified: 2005-06-24 00:14 UTC
Votes:3
Avg. Score:3.7 ± 0.9
Reproduced:3 of 3 (100.0%)
Same Version:0 (0.0%)
Same OS:2 (66.7%)
From: tomas dot matousek at matfyz dot cz Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5CVS, 4CVS (2005-06-19) OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2004-05-24 11:18 UTC] tomas dot matousek at matfyz dot cz
Description:
------------
Why does operator [] create a new array when applied on boolean FALSE and doesn't do so  when applied on integer 0? One of that is IMHO wrong. Either this operator should create a new array on everything that is empty() or it should fail on all scalar types and make a new array only from empty string "" or NULL or uninitialized variable.


Reproduce code:
---------------
$a = 0;
$a[10] = 1;
var_dump($a);

$b = false;
$b[10] = 1;
var_dump($b);


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


Actual result:
--------------
array(1) {
  [10]=>
  int(1)
}

Warning:  Cannot use a scalar value as an array ...


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-05-24 11:21 UTC] tomas dot matousek at matfyz dot cz
Sorry te "Actual result" should be reversed:

Warning:  Cannot use a scalar value as an array ...
array(1) {
  [10]=>
  int(1)
}
 [2004-05-24 19:34 UTC] pollita@php.net
I agree it's inconsistent, as it happens though it's being discussed already because of a related (but not identical) bug report: http://bugs.php.net/28476
 [2005-06-24 00:14 UTC] andi@php.net
In general, I suggest you initialize arrays with $arr = array();.
This behavior is for backwards compatibility which comes from the early PHP 3 and PHP 4 days (before we had NULL and false). The old empty_string mapped to this and we had to keep behavior so as not to break large amounts of applications.
However, you shouldn't be bumping into it especially as you are now aware that you should initialize your array()'s explicitly (or use undefined variables).
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 09:01:28 2024 UTC