php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76294 Variable expansion taking place into string when using undefined constant.
Submitted: 2018-05-02 10:54 UTC Modified: 2018-05-02 11:55 UTC
From: dpastor at comvive dot es Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.2Git-2018-05-02 (Git) OS: Ubuntu Linux 64
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: dpastor at comvive dot es
New email:
PHP Version: OS:

 

 [2018-05-02 10:54 UTC] 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).


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-05-02 10:59 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2018-05-02 10:59 UTC] 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 11:03 UTC] 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 11:07 UTC] requinix@php.net
"$countries[mexico]" is fine. "{$countries[mexico]}" is not.
https://3v4l.org/mfjOY
 [2018-05-02 11:24 UTC] 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:28 UTC] 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:35 UTC] 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:41 UTC] 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:55 UTC] requinix@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 06:01:35 2024 UTC