-2
<?php
$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?

Sandeep
  • 9
  • 3
  • 3
    `echo "Hello {$GLOBALS['x']}";`or `echo "Hello $GLOBALS[x]";` – Jonnix Sep 02 '15 at 14:16
  • 1
    See also: http://php.net/manual/en/language.types.string.php#language.types.string.parsing – mario Sep 02 '15 at 14:17
  • 1
    you can't use quoted array keys in array vars inside double-quoted strings. welcome to PHP, where everything is ad-hoc and consistency doesn't matter. – Marc B Sep 02 '15 at 14:19

1 Answers1

2

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']}";