php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #69010 Accessing bool/int/double/resource with array index returns NULL and no warning
Submitted: 2015-02-08 00:54 UTC Modified: 2016-12-30 16:01 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: mfischer@php.net Assigned: cmb (profile)
Status: Duplicate Package: Arrays related
PHP Version: master-Git-2015-02-08 (Git) OS: OSX 10.10
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: mfischer@php.net
New email:
PHP Version: OS:

 

 [2015-02-08 00:54 UTC] 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


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-02-09 04:01 UTC] laruence@php.net
-Type: Bug +Type: Feature/Change Request
 [2015-02-09 05:12 UTC] laruence@php.net
others are okey, but for NULL..

any undefined variables are NULL
 [2015-02-09 09:24 UTC] 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!
 [2016-12-30 16:01 UTC] cmb@php.net
-Status: Open +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2016-12-30 16:01 UTC] cmb@php.net
Duplicate of request #37676.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 17:01:32 2024 UTC