php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53432 Assignment via string index access on an empty string converts to array
Submitted: 2010-12-01 04:19 UTC Modified: 2010-12-01 15:16 UTC
Votes:7
Avg. Score:2.7 ± 1.3
Reproduced:7 of 7 (100.0%)
Same Version:3 (42.9%)
Same OS:5 (71.4%)
From: phplists at stanvassilev dot com Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: 5.* OS: *
Private report: No CVE-ID:
Have you experienced this issue?
Rate the importance of this bug to you:

 [2010-12-01 04:19 UTC] phplists at stanvassilev dot com
Description:
------------
The title/code says it all. Once a variable is a string, using array access should 
mean char access, but this is not maintained for empty strings, while it's 
maintained for non-empty strings.

Test script:
---------------
$a = ''; // empty string
$a[10] = 'a';
echo $a; // "Array"

$b = ' '; // non empty string
$b[10] = 'b';
echo $b; // "          b"

Expected result:
----------------
"          a"
"          b"

Actual result:
--------------
"Array"
"          b"

Patches

Add a Patch

Pull Requests

Pull requests:

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-12-01 04:33 UTC] phplists at stanvassilev dot com
-Summary: String index access on empty string converts to array +Summary: Assignment via string index access on an empty string converts to array
 [2010-12-01 04:33 UTC] phplists at stanvassilev dot com
I want to note, I realize when the variable is undefined, null, false, array is 
the expected end result, but a string is already signifying explicitly the intent 
of the programmer.

While PHP is a language with loose typing, it's not as loose as to convert scalars 
to arrays and back, despite a set value.
 [2010-12-01 15:16 UTC] jani@php.net
-Status: Open +Status: Verified -Package: Unknown/Other Function +Package: Scripting Engine problem -Operating System: All +Operating System: * -PHP Version: 5.2.14 +PHP Version: 5.*
 
PHP Copyright © 2001-2015 The PHP Group
All rights reserved.
Last updated: Sat Apr 25 19:01:42 2015 UTC