Bug #76294 [Nab]: Variable expansion taking place into string when using undefined constant.
| From: | requinix@php.net | Date: | Wed, 02 May 2018 11:55:30 +0000 |
| Subject: | Bug #76294 [Nab]: Variable expansion taking place into string when using undefined constant. | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-215012@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
Updated by: requinix@php.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:
The first one can be confusing too. I've seen plenty of coders who have notices suppressed so
they don't know that writing $countries[mexico] is wrong - they assume that because it works
(or rather, because it does what they expect it to do) then it must be correct.
Just like with every other tool, it's possible to use PHP correctly and incorrectly, and
sometimes it isn't always obvious to the user which one applies to them. Knowledge is power.
France is bacon.
Previous Comments:
------------------------------------------------------------------------
[2018-05-02 11:41:29] dpastor at comvive dot es
Good to see this is stirring discussion ;).
I just thought, what if...
---------------
<?php
$friends="friends";
$countries['mexico']="land of the good people";
$countries['guatemala']="land of the guatemaltecans";
define('mexico', 'guatemala');
echo $countries[mexico];
$greet="Hi $friends, welcome to $countries[mexico]";
echo $greet;
-----------------
Clearly the first echo will result in "land of the guatemaltecans". To an outsider, it
will be a mistery why the second one (being extremely similar tokens) results in a welcome to
Mexico.
Of course, it is reasonable to state that an understanding of how PHP works is necessary to tackle
this subject, but I just found this amusing.
------------------------------------------------------------------------
[2018-05-02 11:35:25] requinix@php.net
30 minutes ago you said
> this code is simply wrong
> $greet="Hi $friends, welcome to $countries[mexico]";
That is incorrect: the code is fine.
Then you said
> this one does what you think the above does
> $greet="Hi $friends, welcome to {$countries[mexico]}";
I thought you were trying to say that code would work (incorrect), but now I think I misunderstood
and you were trying to say that it will give the warning that @dpastor was expecting to see
(correct).
And yes, your example output does demonstrate the "complex syntax requires quotes like you
want" I said earlier.
And yes, the bug report is about there not being a warning for the simple syntax. What I said
earlier addresses that.
------------------------------------------------------------------------
[2018-05-02 11:28:52] spam2 at rhsoft dot net
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
------------------------------------------------------------------------
[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
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=76294
--
Edit this bug report at https://bugs.php.net/bug.php?id=76294&edit=1