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: 2015-01-09 00:26 UTC
Votes:17
Avg. Score:3.9 ± 1.1
Reproduced:17 of 17 (100.0%)
Same Version:11 (64.7%)
Same OS:9 (52.9%)
From: tgross at m-s dot de Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: * OS: *
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [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.
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC