php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76851 Nonsense error message when "['" used next to variable in string
Submitted: 2018-09-07 17:42 UTC Modified: 2018-09-08 10:07 UTC
Votes:7
Avg. Score:4.1 ± 1.5
Reproduced:4 of 4 (100.0%)
Same Version:3 (75.0%)
Same OS:3 (75.0%)
From: teo8976 at gmail dot com Assigned:
Status: Open Package: *General Issues
PHP Version: 7.3.0beta3 OS:
Private report: No CVE-ID: None
 [2018-09-07 17:42 UTC] teo8976 at gmail dot com
Description:
------------
See below



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

$a='ipsum';

$ss = "lorem $a['foo'] dolor";

Expected result:
----------------
I think this shouldn't throw a syntax error at all, and should result in the value

  "lorem ipsum['foo'] dolor"

being assingned to the variable $ss.

However, if I'm wrong and the use of "$a['foo']" is actually illegal (because of the apostrofe following the opening square bracket next to a variable name), then a SENSIBLE and understandable error message should be thrown.

Actual result:
--------------
Throws this syntax error which makes no sense:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

"unexpected ''" obviously makes no sense. You are stating that the parser unexpectedly found an empty string. While I understand that, internally, this could be the case (the syntax is wrong and hence something that should have been filled with something is instead empty), the error message is not workable, because there's no way I can tell what the unexpectedly empty string is.
'' literally matches everything, so if you tell me "unexpected ''", the offending piece of code could be ANYWHERE (within the given line). 
Not to mention you are implying that '' is a T_ENCAPSED_AND_WHITESPACE.
The parser is erroring out at least one level too deep. Instead of complaining of an unexpected "nothing", it should complain of the unexpected sequence of things that causes that nothing to be nothing.


To me it looks like the unexpected thing here is the "'" right after the "[", so the error should say so.
That is, if that is actually illegal in the first place, which as I said, I'm not even sure of.


Patches

t-error-with-string (last revision 2018-09-08 10:07 UTC by cmb@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-09-08 01:31 UTC] a at b dot c dot de
And what should PHP do if the programmer accidentally quoted the array index inside a double-quoted string? Ignore it and produce bogus output?

One would think the intention of "lorem $a['foo'] dolor" is much more likely to be "lorem {$a['foo']} dolor" than "lorem {$a}['foo'] dolor".

Maybe the error should say "unexpected '''".
 [2018-09-08 09:04 UTC] teo8976 at gmail dot com
> And what should PHP do if the programmer accidentally quoted 
> the array index inside a double-quoted string? Ignore it and 
> produce bogus output?

Probably, yes (that's exactly what happened to me btw). It's not PHP's job to guess the programmer's intentions. What should it do when I accidentally write "$foo_bar" instead of "{$foo}_bar"? Inore the fact that $foo_bar isn't defined and $foo is, and produce empty output? Yep, it's what it should do and what it does (and issue a notice for the use of an undefined variable, just like in our example it would issue a notice for the conversion of array to string).

Anyway, as I said, assuming it's legit to treat it as a parse error (it would probably raise BC concerns to do change it), the unquestionable issue is that the current error message is garbage.
 [2018-09-08 10:07 UTC] cmb@php.net
Indeed, the simple variable substitution syntax does not allow
quotes around array keys (quite consistently with how query
strings are parsed for $_GET).  I agree that the error message for
this case could be improved.
 [2018-09-08 10:07 UTC] cmb@php.net
The following patch has been added/updated:

Patch Name: t-error-with-string
Revision:   1536401258
URL:        https://bugs.php.net/patch-display.php?bug=76851&patch=t-error-with-string&revision=1536401258
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Dec 04 18:01:31 2024 UTC