php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #37676 using Array access operator [] on boolean variable does not show Notice
Submitted: 2006-06-02 11:21 UTC Modified: 2019-07-10 12:19 UTC
Votes:24
Avg. Score:4.0 ± 1.1
Reproduced:22 of 22 (100.0%)
Same Version:14 (63.6%)
Same OS:12 (54.5%)
From: tgross at m-s dot de Assigned: nikic (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: * OS: *
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: tgross at m-s dot de
New email:
PHP Version: OS:

 

 [2006-06-02 11:21 UTC] tgross at m-s dot de
Description:
------------
When trying to access an array using an undefined offset, PHP displays an Notice.

However, when you access a boolean variable with any combination of the array operator [], PHP just returns NULL and displays no error message.

Reproduce code:
---------------
error_reporting (E_ALL);
$a = false;

var_dump ($a[5]);
var_dump ($a['test'][-3]);


Expected result:
----------------
PHP should display a Notice or a Warning.

Actual result:
--------------
PHP displays
NULL NULL

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-06-02 11:25 UTC] tony2001@php.net
Reclassified as feature request.
 [2010-12-20 15:47 UTC] jani@php.net
-Package: Feature/Change Request +Package: Scripting Engine problem -Operating System: Windows XP +Operating System: * -PHP Version: 5.1.4 +PHP Version: *
 [2012-09-29 12:20 UTC] svodev at gmail dot com
The same issue is with NULL values;

$a = NULL;

echo $a['b'];

...and nothing happen
 [2012-09-30 01:06 UTC] laruence@php.net
yeah, I remembered there was a similar bug report of this behavior, and I seems 
made a fix for it.

if I remember correctlly, it was merged into master-branch already
 [2013-02-01 20:12 UTC] jason at fuzzystatic dot com
Bug still occurs on a 5.5-dev version.

example: http://codepad.viper-7.com/rn5kBm
 [2015-01-09 00:26 UTC] ajf@php.net
Still exists in PHP 5.6:

$ php -d error_reporting=E_ALL -r '$x = false; var_dump($x[5]);'
NULL
 [2015-01-09 00:41 UTC] ajf@php.net
Related: https://bugs.php.net/bug.php?id=39915
 [2015-10-07 18:20 UTC] codedokode at gmail dot com
Also it silently returns NULL when trying to pass a number as an array: 

-------------

<?php
 
error_reporting(E_ALL);
$int = 1;
var_dump($int[100]);

-------------

Expected: Error using integer as array
Actual result: NULL

I thing using a scalar value as an array is obviously user's error and it should be reported, e.g. with error message.

Tested in PHP 5.6.7 (Debian), no error message reported. 

The same result if we use float instead of int. The same result if we use resource as array:

$ php -r 'error_reporting(-1);$h = fopen("/etc/passwd", "r"); $x = $h[0];var_dump($x);'
NULL
 [2016-07-25 17:35 UTC] dave at mudsite dot com
I attached a PR to this issue.  It attempts to only display an E_NOTICE once for extended failures like:

$a = false;
$a[1][2][3][4];

This should error once.  It will also raise an E_NOTICE for other FETCH_DIM_R opcodes like:

$a = false;
list($b, $c) = $a;

This would have 2 E_NOTICE one for each assignment to $b, and $c.
 [2019-07-10 12:19 UTC] nikic@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: nikic
 [2019-07-10 12:19 UTC] nikic@php.net
Will throw a notice in 7.4.
 [2019-08-31 20:12 UTC] glen at pld-linux dot org
Trying to find RFC or PR or commit related to this bug...

- https://wiki.php.net/rfc/notice-for-non-valid-array-container
- from https://github.com/php/php-src/pull/2031#issuecomment-238315238
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 15:01:28 2024 UTC