Wednesday, 18 April 2018

php - Why Superglobals can't be printed in double quotes?




$x = "world";
echo "hello $x";
echo "hello $GLOBALS['x']";
?>


In the above code the first statement prints hello world but second doesn't print anything.Why superglobals are not accessible in double quotes?


Answer



It's not that super globals can't it's that you can't access items in an array in the string. If you did the following it'll work fine.



echo "hello {$GLOBALS['x']}";

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...