php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52184 Using null as array gives no warning or error
Submitted: 2010-06-25 13:39 UTC Modified: 2010-06-25 15:25 UTC
From: egorinsk at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.2 OS: linux
Private report: No CVE-ID: None
 [2010-06-25 13:39 UTC] egorinsk at gmail dot com
Description:
------------
Using null as array gives no warning, notice or error, just returns null. It is at least strange.

Test script:
---------------
<?php

error_reporting(E_ALL);

$a = null;
echo $a[12]; // Gives no warning, notice or error

?>

Expected result:
----------------
Some kind of notice or warning.

Actual result:
--------------
(nothing)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-25 15:25 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2010-06-25 15:25 UTC] johannes@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

You aren't accessing an array offset but a string offset. null is casted to String. The string is shorter than the offset (length is 0) so it returns NULL.
 [2010-06-26 00:04 UTC] egorinsk at gmail dot com
Thank you for your response.

> You aren't accessing an array offset but a string offset. null is casted to String. The string is shorter than the offset (length is 0) so it returns NULL.

Well, PHP emits a notice when accessing uninitialized string offset, so this is not the case:

~: php -r 'error_reporting(E_ALL); $a=""; echo $a[12]; '
PHP Notice:  Uninitialized string offset: 12 in Command line code on line 1
PHP Stack trace:
PHP   1. {main}() Command line code:0

Notice: Uninitialized string offset: 12 in Command line code on line 1

Call Stack:
    0.0002     317032   1. {main}() Command line code:0


Anyway, I guess there should be at least warning, because null is neither a string, nor array, and trying to access element of null is obviously an error, and I, as a developer, would prefer to have it logged.
 [2010-07-01 03:43 UTC] anon at anon dot com
He has an interesting point.
For arrays you get "undefined offset".
For strings, "uninitialized string offset".
For objects, "cannot use object of type foo as array".
For null, numbers, bools, no error, no nothing.
 [2019-04-04 13:24 UTC] d dot mathis at firegroup dot com
This has been marked as not a bug.
Yet compared to all other Erros and Notices this should also throw at least a notice.

If this had been cast to a string, it would throw a "Notice: Uninitialized string offset:"

<?php
$array = null;
$value = $array['one']['two'];
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 02 15:01:33 2024 UTC