1

The $GLOBALS array of PHP provides access to all global variables, like

<?php
$foo = 'hello';
function myFunc() {
  echo $GLOBALS['foo']; // prints "hello"
}
?>

Now I have to work on some code from other people that directly adds elements to the array to have it available globally (without having an according variable), like so:

<?php
function doSomething() {
  // $newData is NOT existing anywhere!
  $GLOBALS['newData'] = 'Hello World!';
  // now $GLOBALS['newData'] is available anywhere else w/o actual variable
}
?>

Until now I never saw this specific usage of the $GLOBALS array and was wondering if it is considered "safe" or "good"? The PHP manual makes no statement about writing to this array, only about reading from it.

Foo Bar
  • 1,764
  • 4
  • 24
  • 43
  • What make you think that its not a good practice ? – Raheel Mar 09 '15 at 13:19
  • 2
    @RaheelKhan Example case: one developer declares a global variable with the same name that another developer uses for another purpose directly in the array. – Foo Bar Mar 09 '15 at 13:20
  • 1
    It's not a good practice to use global variables in the first place. – axiac Mar 09 '15 at 13:29
  • @axiac Yes, but I can not refactor the whole code now. But at least it's in the time plan to clean up some of the smaller problems. That's why I ask, if using `$GLOBALS` this way actually is considered to be a problem. – Foo Bar Mar 09 '15 at 13:50
  • It doesn't really matter whether you use `$GLOBALS` or `global`, globals in general are bad. Closed as dupe of question which should explain why. – deceze Mar 09 '15 at 13:51
  • @deceze: My question was NOT if globals are bad or good. It was a specific question about the details of the PHP language construct `$GLOBALS` that are not clearly stated in the official manual. – Foo Bar Mar 09 '15 at 13:53
  • 2
    It's really only a syntactical difference to using `global $foo`. It's as bad (or good) for the same reasons. Proof: http://3v4l.org/K1p04 – deceze Mar 09 '15 at 13:55
  • @FooBar as others already mentioned in comments, `global $foo` and `$GLOBALS['foo']` have the same meaning. It's just a matter of taste which one you use. I would prefer using the `$GLOBALS` syntax because this way it's more visible that the variable is global and changing it here (in the function) might have unexpected effects at the other side of the universe. Or it will just wait around the next corner and bite me. Ouch! – axiac Mar 09 '15 at 14:27
  • 1
    @FooBar A declaration `global $foo;` creates an entry named `foo` in the table of local variables that points to the variable known in the global context as `foo` (which can be accessed from any content as `$GLOBALS['foo']`). If the global variable `$foo` doesn't exist then it is created. You can think of `global $foo;` as an equivalent of `$foo = & $GLOBALS['foo'];`. Take a look at this sample code: http://3v4l.org/KSpj3 – axiac Mar 09 '15 at 14:57

2 Answers2

-1

Opinion perhaps, but I would think it's better instead to use define instead of the $GLOBALS array if you're looking to define a constant for use in other parts of your application.

PHP:define - Manual

Adam T
  • 675
  • 8
  • 22
  • Note that `define` does not create *variables*, but constants. – deceze Mar 09 '15 at 13:52
  • @deceze Original answer was corrected. Not sure, but if possible, can the downvote please be overturned? Thanks. – Adam T Mar 10 '15 at 16:34
  • @deceze Thanks for your response. I'll ask then (hoping the down voter sees this) - I've corrected my answer, please remove or revert the down vote. Thanks, much appreciated. – Adam T Mar 10 '15 at 17:19
-3
<?php
function doSomething() {
  $_POST['__newData'] = 'Hello World!';
}

doSomething();
echo $_POST['__newData'];
?>
Buse Gönen
  • 238
  • 1
  • 5