Bug #73081 [Opn->Nab]: strange behavior when assigning string key on empty string
| From: | nikic@php.net | Date: | Wed, 14 Sep 2016 10:24:54 +0000 |
| Subject: | Bug #73081 [Opn->Nab]: strange behavior when assigning string key on empty string | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-204028@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=73081&edit=1
ID: 73081
Updated by: nikic@php.net
Reported by: jhdxr@php.net
Summary: strange behavior when assigning string key on empty
string
-Status: Open
+Status: Not a bug
Type: Bug
Package: *General Issues
PHP Version: 7.1.0RC1
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2016-09-14 10:21:33] 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 06:10:03] 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"
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=73081&edit=1