php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #45290 always throw an E_NOTICE when accessing an undefined index
Submitted: 2008-06-17 04:24 UTC Modified: 2012-02-24 17:01 UTC
Votes:3
Avg. Score:3.7 ± 1.2
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: cole dot snodgrass at gmail dot com Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: 5.2.6 OS: linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: cole dot snodgrass at gmail dot com
New email:
PHP Version: OS:

 

 [2008-06-17 04:24 UTC] cole dot snodgrass at gmail dot com
Description:
------------
It seems strange that when the strictest level or error reporting is enabled an E_NOTICE is thrown when attempting to access a non-existent key of an array, but nothing is thrown when attempting to access a non-existent key of a bool, or a string, or it appears any other variable type.  An E_NOTICE will also be thrown if attempting to access a non-existent numerical key in a string, but not if attempting to access a non-existent non-numerical key.

If an E_NOTICE is displayed under one circumstance for an index not existing it seems that it should be displayed under all circumstances when an index doesn't exist.

Reproduce code:
---------------
error_reporting(E_ALL | E_STRICT);

$dummyArray = array();
// this will throw an E_NOTICE
$error = $dummyArray['doesNotExist'];

$dummyArray = true;
// this will not throw an E_NOTICE
$noError = $dummyArray['doesNotExist'];

$dummyArray = 'string';
// this will not throw an E_NOTICE
$noError = $dummyArray['doesNotExist'];
// but this will throw an E_NOTICE
$error = $dummaryArray['100'];


Expected result:
----------------
Notice: Undefined index: doesNotExist
Notice: Undefined index: doesNotExist
Notice: Undefined index: doesNotExist
Notice: Uninitialized string offset:  100

Actual result:
--------------
Notice: Undefined index: doesNotExist
< nothing when attempting to access a non-existent key of a bool >
< nothing when attempting to access a non-existent key of a string >
Notice: Uninitialized string offset:  100

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-06-17 16:13 UTC] colder@php.net
FWIW: $string['foo'] is equivalent to $string[0] and in your case it won't trigger any notice indeed because the offset 0 is defined.
 [2011-01-27 15:06 UTC] bugs dot php dot net at moesen dot nu
See also:
* Bug #48560 - E_STRICT does not report error if booleans or ints are used as arrays
* Request #37676 - using Array access operator [] on boolean variable does not show Notice

Dupe it to the latter?
 [2012-02-24 17:01 UTC] nikic@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: nikic
 [2012-02-24 17:01 UTC] nikic@php.net
$string['foo'] will raise a notice as of PHP 5.4, so I'd say this is fixed, so I'm closing here.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 14 02:01:32 2025 UTC