php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #67750 Subscript Operator claims empty brackets invalid for string
Submitted: 2014-08-02 16:16 UTC Modified: 2014-08-03 11:33 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 1 (0.0%)
From: ralph-bugsphpnet at inputplus dot co dot uk Assigned:
Status: Closed Package: PHP Language Specification
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2014-08-02 16:16 UTC] ralph-bugsphpnet at inputplus dot co dot uk
Description:
------------
https://github.com/php/php-langspec/blob/master/spec/10-expressions.md#subscript-operator says

    *expression* can be omitted only if *subscript-expression* is used in a
    modifiable-lvalue context and *postfix-expression* does not designate a
    string.

Yet this is valid:

    $a =  ""; $a[] = "y"; var_dump($a);

I've omitted expression, postfix-expression is a string, just an empty one.
The result is array("y");  the original empty string disappears.

See http://3v4l.org/vg7Oe for more.

Test script:
---------------
<?php
$a =  ""; $a[] = "y"; var_dump($a);
$b = "x"; $b[] = "y"; var_dump($b);


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-08-03 02:36 UTC] stas@php.net
"" is a tricky case because in this case $a is converted to array. So strictly speaking [] is not applied to a string, it applies to an array, to which empty string is auto-converted.
 [2014-08-03 11:33 UTC] ralph-bugsphpnet at inputplus dot co dot uk
stas, I respectfully disagree.  Firstly,
https://github.com/php/php-langspec/blob/master/spec/08-conversions.md#converting-to-array-type
says

    If the source type is bool, int, float, or string, the result value
    is an array of one element whose type and value is that of the
    source.
    
but that's not happening here as

    $a =  ""; $a[] = "y";
    
gives

    array(1) { [0]=> string(1) "y" }

whereas the conversion of the empty string to an array with an empty 
string as its first element should have given

    array(2) { [0]=> string(0) "", [0]=> string(1) "y" }

So the empty string is becomming an empty array.  I don't see this
possibility stated anywhere. 

"Subscript Operator" doesn't say auto-conversion applies.  If that's 
really what's happening here, then shouldn't it?
 [2014-08-04 10:27 UTC] datibbaw@php.net
The values of false, null and "" are all converted to an empty array and thus do not follow the traditional casting rules.

This is also evident from the source code: http://lxr.php.net/xref/PHP_5_6/Zend/zend_execute.c#1160

This should probably be mentioned in the spec.
 [2015-06-28 20:56 UTC] stas@php.net
Automatic comment on behalf of smalyshev@gmail.com
Revision: http://git.php.net/?p=php-langspec.git;a=commit;h=43914c041e5dfe7a5b53d8350c5c2b287b1ade55
Log: Fix bug #67750
 [2015-06-28 20:56 UTC] stas@php.net
-Status: Open +Status: Closed
 [2015-06-28 23:20 UTC] stas@php.net
Automatic comment on behalf of smalyshev@gmail.com
Revision: http://git.php.net/?p=php-langspec.git;a=commit;h=43914c041e5dfe7a5b53d8350c5c2b287b1ade55
Log: Fix bug #67750
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Oct 12 07:01:28 2024 UTC