Req #69010 [Opn->Dup]: Accessing bool/int/double/resource with array index returns NULL and no warning
| From: | cmb@php.net | Date: | Fri, 30 Dec 2016 16:01:33 +0000 |
| Subject: | Req #69010 [Opn->Dup]: Accessing bool/int/double/resource with array index returns NULL and no warning | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-206245@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69010&edit=1
ID: 69010
Updated by: cmb@php.net
Reported by: mfischer@php.net
Summary: Accessing bool/int/double/resource with array index
returns NULL and no warning
-Status: Open
+Status: Duplicate
Type: Feature/Change Request
Package: Arrays related
Operating System: OSX 10.10
PHP Version: master-Git-2015-02-08 (Git)
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
Duplicate of request #37676.
Previous Comments:
------------------------------------------------------------------------
[2015-02-09 09:24:48] mfischer@php.net
I thought so initially too, but when something returns false or NULL for "doesn't
exist" and you miss the check it, you happily continue without noticing. I.e. in real life
developers create buggy code and whether you erroneously try to access an index of a
"false" or "NULL" is the same pain in practice.
<?php
$result = some_important_func();
# I'm a bad developer, I /forgot/ to check $result
$id = $result['id'];
Now $id is NULL, doesn't natter $result was false or NULL.
But I understand this probably warrants discussion!
------------------------------------------------------------------------
[2015-02-09 05:12:04] laruence@php.net
others are okey, but for NULL..
any undefined variables are NULL
------------------------------------------------------------------------
[2015-02-08 00:54:16] mfischer@php.net
Description:
------------
Accessing booleans, integers and resources with an array index returns NULL but emits no
warning/notice of a potential error.
Tested with
$ sapi/cli/php -v
PHP 7.0.0-dev (cli) (built: Feb 8 2015 01:14:57)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v3.0.0-dev, Copyright (c) 1998-2015 Zend Technologies
I made up the notice messages in "Expected result" to put emphasis what I would have
expected.
Similar report: https://bugs.php.net/bug.php?id=44821 , categorized
as documentation problem which doesn't sound right.
Test script:
---------------
<?php
$a = false;
$b = $a['index'];
var_dump($b);
$a = 1;
$b = $a['index'];
var_dump($b);
$a = 1.1;
$b = $a['index'];
var_dump($b);
$a = NULL;
$b = $a['index'];
var_dump($b);
$a = fopen($argv[0], 'r');
$b = $a['index'];
var_dump($b);
Expected result:
----------------
Notice: Illegal array access on boolean in Command line code on line 1
NULL
Notice: Illegal array access on integer in Command line code on line 1
NULL
Notice: Illegal array access on double in Command line code on line 1
NULL
Notice: Illegal array access on NULL in Command line code on line 1
NULL
Notice: Illegal array access on resource in Command line code on line 1
NULL
Actual result:
--------------
NULL
NULL
NULL
NULL
NULL
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69010&edit=1