Bug #67750 [Opn]: Subscript Operator claims empty brackets invalid for string

From: Date: Sun, 03 Aug 2014 11:33:33 +0000
Subject: Bug #67750 [Opn]: Subscript Operator claims empty brackets invalid for string
References: 1  Groups: php.standards 
Request: Send a blank email to standards-+get-273@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=67750&edit=1 ID: 67750 User updated by: ralph-bugsphpnet at inputplus dot co dot uk Reported by: ralph-bugsphpnet at inputplus dot co dot uk Summary: Subscript Operator claims empty brackets invalid for string Status: Open Type: Bug Package: PHP Language Specification PHP Version: Irrelevant Block user comment: N Private report: N New Comment: 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? Previous Comments: ------------------------------------------------------------------------ [2014-08-03 02:36:38] 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-02 16:16:45] 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); ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=67750&edit=1

« previous php.standards (#273) next »