Bug #76851 [Com]: Nonsense error message when "['" used next to variable in string

From: Date: Sat, 08 Sep 2018 01:31:58 +0000
Subject: Bug #76851 [Com]: Nonsense error message when "['" used next to variable in string
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-216923@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=76851&edit=1 ID: 76851 Comment by: a at b dot c dot de Reported by: teo8976 at gmail dot com Summary: Nonsense error message when "['" used next to variable in string Status: Open Type: Bug Package: *General Issues PHP Version: 7.3.0beta3 Block user comment: N Private report: N New Comment: 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 '''". Previous Comments: ------------------------------------------------------------------------ [2018-09-07 17:42:18] 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. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=76851&edit=1

« previous php.bugs (#216923) next »