php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #34341 No warning for $a['zz'] if $a is not an assoc array
Submitted: 2005-09-02 03:44 UTC Modified: 2013-07-31 03:15 UTC
Votes:5
Avg. Score:4.0 ± 0.9
Reproduced:4 of 4 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: herve dot pages at laposte dot net Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 4.4.0 OS: Fedorac Core 3
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: herve dot pages at laposte dot net
New email:
PHP Version: OS:

 

 [2005-09-02 03:44 UTC] herve dot pages at laposte dot net
Description:
------------
PHP doesn't output any warning for $a['zz'] if $a is not an assoc array.
This problem has been reported in Bug #33879 in the particular case where $a is a string. Since individual chars in a string can be accessed using the $a[] syntax, and since the key 'zz' will be automatically cast to 0, then I can understand why PHP will accept $a['zz'] without complaining.
But when $a is an integer, or a bool, or anything else that has nothing to do with an array (or a string), I don't see any reason why PHP should accept $a['zz'] without complaining! It would make some bugs much easier to find...

Reproduce code:
---------------
<?php
error_reporting(E_ALL);

$assoc = array();
$test = $assoc['zz']; // Warning "Undefined index: zz..." issued. OK

// Now what if the $assoc variable is replaced by a variable that is not
// an assoc array?
// The expected/natural behaviour would be that PHP complains even
// stronger because if such situation were occuring with a compiled
// language, any descent compiler would not even compile.

$not_an_assoc = 85;
$test = $not_an_assoc['zz']; // No warning issued!
echo gettype($test); // -> Display NULL

$not_an_assoc = "85";
$test = $not_an_assoc['zz']; // No warning issued!
echo $test; // -> Display 8
?>

Expected result:
----------------
It would be nice if PHP could tell me I'm doing something wrong with a warning or an error (that's why I'm using error_reporting(E_ALL) after all).


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-31 03:15 UTC] yohgaki@php.net
-Status: Open +Status: Wont fix -Package: Feature/Change Request +Package: *General Issues
 [2013-07-31 03:15 UTC] yohgaki@php.net
String offset access is defined now, but
PHP does not raise errors for scalar types. (e.g. integer, bool)

Raising errors for this kind of access will break many codes. It's unlikely this 
behavior will be changed.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 11:01:33 2024 UTC