Bug #76294 [Com]: Variable expansion taking place into string when using undefined constant.
| From: | spam2 at rhsoft dot net | Date: | Wed, 02 May 2018 11:28:53 +0000 |
| Subject: | Bug #76294 [Com]: Variable expansion taking place into string when using undefined constant. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-215009@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=76294&edit=1
ID: 76294
Comment by: spam2 at rhsoft dot net
Reported by: dpastor at comvive dot es
Summary: Variable expansion taking place into string when
using undefined constant.
Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: Ubuntu Linux 64
PHP Version: 7.2Git-2018-05-02 (Git)
Block user comment: N
Private report: N
New Comment:
and that's what this bugreport is about: "Warning: Use of undefined constant mexico -
assumed 'mexico'" missing
php > echo "welcome to $countries[mexico] test";
welcome to MEXICO test
Previous Comments:
------------------------------------------------------------------------
[2018-05-02 11:24:28] spam2 at rhsoft dot net
nonsense - i have thousands lines of code in the style of " {$arr['whatever']} "
and as you can see below it's correct
php > $countries = ['mexico'=>'MEXICO'];
php > echo "welcome to {$countries['mexico']} test";
welcome to MEXICO test
php > echo "welcome to {$countries[mexico]} test";
Warning: Use of undefined constant mexico - assumed 'mexico' (this will throw an Error in
a future version of PHP) in php shell code on line 1
welcome to MEXICO test
php >
------------------------------------------------------------------------
[2018-05-02 11:07:39] requinix@php.net
"$countries[mexico]" is fine. "{$countries[mexico]}" is not.
https://3v4l.org/mfjOY
------------------------------------------------------------------------
[2018-05-02 11:03:33] spam2 at rhsoft dot net
this code is simply wrong
$greet="Hi $friends, welcome to $countries[mexico]";
this one does what you think the above does
$greet="Hi $friends, welcome to {$countries[mexico]}";
------------------------------------------------------------------------
[2018-05-02 10:59:47] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
Quotes aren't necessary for the simple interpolation syntax because that's just how
it's always been. Complex syntax requires quotes like you want.
http://php.net/manual/en/language.types.array.php
http://php.net/manual/en/language.types.string.php
------------------------------------------------------------------------
[2018-05-02 10:54:13] dpastor at comvive dot es
Description:
------------
Code using array string indexes without quotes is interpreted in newer versions of PHP as undefined
constants. However, the same behaviour is not observed when variable expansion takes place.
See the example code.
Test script:
---------------
<?php
$friends="friends";
$countries['mexico']="land of the good people";
//echo $countries[mexico] will result in a warning.
//This, however, results in the correct expansion.
$greet="Hi $friends, welcome to $countries[mexico]";
echo $greet;
Expected result:
----------------
Unless I am mistaken, I expect the parser to be consistent and tell me that $countries[mexico] is
trying to reference an undefined constant when using it inside variable expansion (as it does
outside of it).
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=76294&edit=1