php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73081 strange behavior when assigning string key on empty string
Submitted: 2016-09-14 06:10 UTC Modified: 2016-09-14 10:24 UTC
From: jhdxr@php.net Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.1.0RC1 OS:
Private report: No CVE-ID: None
 [2016-09-14 06:10 UTC] jhdxr@php.net
Description:
------------
previously, empty string converted to array automaticly when using array related operators on empty string. And I know this behaviour was changed because of the fix for https://bugs.php.net/bug.php?id=53432 .
there are some similar or related bugs like https://bugs.php.net/bug.php?id=72430 , which I think reasonable, since empty string will not be converted to array, and [] is an operator for array rather than string.

however, when I try to assign a value via string index on an empty string, I just get a warning and strange result (see the test script below <https://3v4l.org/Kuj6l>). 


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

$arr = '';
$arr['a'] = 'test';

var_dump($arr);

Expected result:
----------------
//since $arr is a string, array operator should not be allowed
Fatal error:  

or

//bug #53432 only covers the cases of using number as the index, and in other cases, maybe empty string should be converted to an array.
array(1) { ["a"]=> string(4) "test" } 


Actual result:
--------------
Warning: Illegal string offset 'a' in /in/Kuj6l on line 4 
string(1) "t"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-09-14 10:21 UTC] cmb@php.net
Firstly, the index operator ([]) is not only an array operator,
but also a string operator[1], so it makes sense to treat [] on
strings as string operations.

Whether the empty index operator should be supported for strings
had been discussed on internals[2], and IIRC there had been
consensus to not allow it at all. I'm not sure if using the index
operator on strings with string indixes has explicitly been
discussed, but I would rather not treat this differently, to avoid
subtle differences (e.g. $str['1'] vs $str[1]). Instead the
current behavior looks fine for me, because it uses PHP type
juggling rules to convert a string index to a numeric index (in
this case (int)'a' => 0).

So, in my opinion, this is not a bug.

[1] <http://php.net/manual/en/language.types.string.php#language.types.string.substr>
[2] <http://news.php.net/php.internals/91200>
 [2016-09-14 10:24 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2016-09-14 10:24 UTC] nikic@php.net
This behavior is due to three factors:

a) $str['a'] behaves as $str[(int) 'a'], i.e. $str[0], together with an "Illegal string offset" warning.
b) Assigning a string longer than 1 to a string offset, will assign the first character of the string.
c) Assigning an offset past the end of a string will extend the string to that offset with whitespace and then perform the assignment.

As such, the behavior is correct under the current semantics. However, it may be time to reevaluate some of those, e.g. make a) a hard error or make b) throw a warning.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 00:01:29 2024 UTC