php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52194 incorrect behaviour when printing non existend array index
Submitted: 2010-06-26 18:29 UTC Modified: 2010-06-26 20:28 UTC
From: bubi1 at mailinator dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.2 OS: all
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: bubi1 at mailinator dot com
New email:
PHP Version: OS:

 

 [2010-06-26 18:29 UTC] bubi1 at mailinator dot com
Description:
------------
The code below is self explaining.

Test script:
---------------
php -r '$arr = array('test');var_dump($arr[0][0]);var_dump(isset($arr[0][0]));'

Expected result:
----------------
NULL
bool(false)

Actual result:
--------------
string(1) "t"
bool(true)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-06-26 19:12 UTC] rasmus@php.net
-Status: Open +Status: Bogus
 [2010-06-26 19:12 UTC] rasmus@php.net
A string is an array of characters.  You have:

$arr[0] = "test";

Therefore array index 0 on that string is:

$arr[0][0] which very logically is "t"
 [2010-06-26 19:44 UTC] bubi1 at mailinator dot com
Yes, i know that a string is an array of chars. Maybe i'm wrong,but i remember once only the syntax $string{0} was working, non $string[0] also.

Anyway, why this works?
php -r '$arr = 'test';var_dump($arr['rasmus']);'
still prints "t".

There is no key called "rasmus" in the $arr string...
 [2010-06-26 19:56 UTC] dtajchreber@php.net
Because non-integer keys are converted to integers. Everything is explained here: 
http://www.php.net/manual/en/language.types.string.php
 [2010-06-26 20:28 UTC] bubi1 at mailinator dot com
Yes, you are righ, i have found that.
"Warning
[...] Non-integer types are converted to integer. [...]"

However i find this behavior a bit strange. If the string is an array, it should behave as an array, and generate an error on a non existend key.
Otherwise, if do not know if $arr is a string or array, i have to use 3 functions for checking the existence of a value: is_array($arr) && array_key_exists('smth', $arr) && isset($arr['smth']), when i could simply use isset($arr['smth']) if things worked as expected.

But ok, nevermind. A documented bug is a feature :)
Sorry for opening this thread.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Fri Aug 14 23:00:01 2026 UTC