|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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). PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 22:00:01 2025 UTC |
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]}";"$countries[mexico]" is fine. "{$countries[mexico]}" is not. https://3v4l.org/mfjOYnonsense - 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 >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.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.