php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #71611 array index starting with number in double quote string
Submitted: 2016-02-16 18:27 UTC Modified: 2021-10-08 17:35 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:-1 (-33.3%)
From: asylow at free dot fr Assigned:
Status: Open Package: *General Issues
PHP Version: 7.1 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: asylow at free dot fr
New email:
PHP Version: OS:

 

 [2016-02-16 18:27 UTC] asylow at free dot fr
Description:
------------
In a double quoted string, an array index beginning with numbers followed by letters  is not correctly interpreted.

The documentation regarding Nowdocs says : "With array indices, the closing square bracket (]) marks the end of the index." but the parser seems to stop at the first alpha character.

Of course this can be treated with the "Complex (curly) syntax" but "1a" is not more complex than "a1".



Test script:
---------------
$array['a1'] = 'foo';
echo "$donnees[a1]";
// foo

$array['1a'] = 'foo';
echo "$donnees[1a]";
// Parse error: syntax error, unexpected 'a' (T_STRING), expecting ']'



Expected result:
----------------
foo
foo

Actual result:
--------------
Parse error: syntax error, unexpected 'a' (T_STRING), expecting ']'

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-02-16 23:23 UTC] yohgaki@php.net
This may be fixed, but working syntax is

echo "${array['1a']}";
or
echo "{$array['1a']}";

You should use {} for complex expression. We need improvement for string offset, though.

php > echo "${str{1}}";
PHP Parse error:  syntax error, unexpected '{' in php shell code on line 1

php > echo "{$str{1}}";
b
 [2018-09-09 01:14 UTC] carusogabriel@php.net
The change for this bug may also affect the way we declare variables starting with a number: https://3v4l.org/dVGQD
 [2018-09-09 01:27 UTC] requinix@php.net
-PHP Version: 5.6.18 +PHP Version: 7.1
 [2018-09-09 01:27 UTC] requinix@php.net
> the parser seems to stop at the first alpha character
Keys like "a1a" work. The parser is deciding whether the key is a string or number based on the first character: starts with a letter then it's a string, starts with a number then it's a number. So "1a" looks like a number at first, but then it hits the 'a' and gets confused.

Given the normal string/int type juggling with array keys, I suspect treating the key always as a string would work.
 [2021-10-08 17:35 UTC] cmb@php.net
-Type: Bug +Type: Documentation Problem
 [2021-10-08 17:35 UTC] cmb@php.net
It's actually slightly different.  The scanner is regex based, and
if it is in ST_VAR_OFFSET state, it checks for a decimal number
(and matches 1), so it yields a numeric string.  Then it proceeds,
and matches a label (a), and yields it as string.  The parser
doesn't expect that, though.  This might be fixable, but I see no
point in doing that, especially since the complex interpolation
syntax already supports this, and the docs state[1]:

| For anything more complex, you should use the complex syntax.

We need to document, though, what is more complex, i.e. what
exactly is supported as array index for the simple syntax.

[1] <https://www.php.net/manual/en/language.types.string.php#language.types.string.parsing>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 11:01:30 2024 UTC