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:8
Avg. Score:2.9 ± 1.3
Reproduced:7 of 8 (87.5%)
Same Version:3 (42.9%)
Same OS:5 (71.4%)
From: phplists at stanvassilev dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5.* OS: *
Private report: No CVE-ID: None
 [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

chat (last revision 2016-07-04 09:11 UTC by m dot anfasmujeeb32 at gmail dot com)

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.*
 [2016-06-04 17:20 UTC] nikic@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=945a83103de8fbba66bdf643c4701263854c0056
Log: Fixed bug #53432
 [2016-06-04 17:20 UTC] nikic@php.net
-Status: Verified +Status: Closed
 [2016-07-20 11:30 UTC] davey@php.net
Automatic comment on behalf of nikic
Revision: http://git.php.net/?p=php-src.git;a=commit;h=945a83103de8fbba66bdf643c4701263854c0056
Log: Fixed bug #53432
 [2017-07-14 16:55 UTC] christian at deligant dot net
Bug is still present on 7.0.18 (ubuntu 16-04 vanilla)

the only difference is that the string comes from a database but is definitely an empty string ('') nothing more
 [2018-11-22 18:21 UTC] cmb@php.net
Automatic comment from SVN on behalf of cmb
Revision: http://svn.php.net/viewvc/?view=revision&revision=346032
Log: Fix #73503: Missing entry in the migration guide

Since the fix for bug #53432 only targeted master, we document
it in the migration guide as incompatible change.

This also integrates user note 120336.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 04:01:31 2024 UTC