php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30885 Accessing string characters with dubious array indexes
Submitted: 2004-11-24 19:01 UTC Modified: 2004-11-24 23:13 UTC
From: php-bug-2004 at ryandesign dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.0.2 OS: N/A
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: php-bug-2004 at ryandesign dot com
New email:
PHP Version: OS:

 

 [2004-11-24 19:01 UTC] php-bug-2004 at ryandesign dot com
Description:
------------
Accessing a character of a string using the deprecated 
array notation when the array index is not a number does 
not produce a notice or warning, but I believe it 
should.

If I write $test['some string'] = '234' what I probably 
want to do is to have $test be an array with a key 
'some string' and a value '234', but if $test was 
already a string, PHP instead sets character index 0 of 
the string to "2". PHP should issue an error of some 
kind, alerting the programmer that $test is being used 
as a string, so that the programmer can properly 
initialize the array with $test = array() before filling 
it.

Reproduce code:
---------------
$test = 'blah';

$test[2] = 'x';
// sets the third character of $test to "x"

echo $test . "<br />\n";
// outputs "blxh"

$test['some string'] = '234';
// sets the first character of $test to "2"

echo $test . "<br />\n";
// outputs "2lxh"

Expected result:
----------------
Warning: invalid non-numeric index into string $test


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-11-24 19:41 UTC] php-bug-2004 at ryandesign dot com
I'm sorry -- I just found the bug of which this one is a 
duplicate:

http://bugs.php.net/bug.php?id=29271

But I am not satisfied with the resolution of that bug 
("bogus"). The problem is that when I said $test['some 
string'] = '234' I _wanted_ to create an associative 
array -- and if $test had been undefined, that would 
have happened. But because $test was already a string, 
the behavior was different and undesired. The problem is 
that $test was expected to be undefined at that point in 
the code, but due to an unexpected set of circumstances 
actually turned out to be a string. The amount of 
debugging that was necessary to discover this mistake 
was non-trivial, and it could have been prevented if PHP 
had issued a warning.
 [2004-11-24 23:13 UTC] iliaa@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

this is expected.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jul 27 19:00:03 2025 UTC