Req #79044 [Opn]: Curly Braces Throw T_LNUMBER Error

From: Date: Sun, 29 Dec 2019 21:21:15 +0000
Subject: Req #79044 [Opn]: Curly Braces Throw T_LNUMBER Error
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-224597@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79044&edit=1

 ID:                 79044
 User updated by:    thebluewalrus at gmail dot com
 Reported by:        thebluewalrus at gmail dot com
 Summary:            Curly Braces Throw T_LNUMBER Error
 Status:             Open
 Type:               Feature/Change Request
 Package:            Compile Failure
 Operating System:   Mac OSX 10.12.4
 PHP Version:        7.4.1
 Block user comment: N
 Private report:     N

 New Comment:

Yes, I agree I would expect '"$1"' and '"{$1}"' to behave
the same but they don't. I think one or the other is functioning incorrectly. Simplifying the
"correct" example it still executes:

preg_replace('/(a)/', "$1", 'a');


Previous Comments:
------------------------------------------------------------------------
[2019-12-29 16:23:30] bugreports at gmail dot com

you can't have variables starting with a number in PHP

-----------------------

Correct behavior:
preg_replace('/(a)/', "$1test", 'a');

i would say this is not correct behavior and should trhow the same error becasue you can't have
a variable $1test nor can you have $1

-----------------------

php > $1test = 'test';

Parse error: syntax error, unexpected '1' (T_LNUMBER), expecting variable (T_VARIABLE) or
'{' or '$' in php shell code on line 1

------------------------------------------------------------------------
[2019-12-29 15:52:26] thebluewalrus at gmail dot com

Description:
------------
Using curly braces and a backreference in double quotes in regex replacement produces a
'T_LNUMBER' error for an invalid variable name. This behavior is inconsistent with other
behavior though, e.g. uncurled the "variable" doesn't throw parser error.

Test script:
---------------
Correct behavior:

preg_replace('/(a)/', "$1test", 'a');

Incorrect behavior:

preg_replace('/(a)/', "{$1}test", 'a');

Expected result:
----------------
In regex context this behaviors is incorrect because '{$3}' should produce '{capture
group three value}', as it does outside the curly braces.

Actual result:
--------------
Parse error: syntax error, unexpected '1' (T_LNUMBER), expecting variable (T_VARIABLE) or
'{' or '$'


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=79044&edit=1


Thread (9 messages)

« previous php.bugs (#224597) next »