1

As per my knowledge, any variable declared outside the function is treated as a 'Global Variable' in PHP.

To access such global variable inside the function there are two ways one is to declare is as global inside the function with the keyword 'global'. Another way is to access the global variable as an index of superglobal variable as $GLOBALS['global variable'].

Both of the above mentioned ways do the same thing and both are valid.

But in following two programs this assumption seems to fail as both the programs are generating different outputs. I want to clear this doubt whether this assumption is 100% true or it works sometimes and sometimes not.

Please go through the following code snippets and their respective results :

Code Snippet 1 :

<?php

  function destroy_foo() {
    global $foo;
    unset($foo);
  }

  $foo = 'bar';
  destroy_foo();
  echo $foo;
?>

Output of Code Snippet 1 :

bar

Code Snippet 2 :

<?php

  function destroy_foo() {
    unset($GLOBALS['foo']);
  }

  $foo = 'bar';
  destroy_foo();
  echo $foo;
?>

Output of Code Snippet 2 :

Notice: Undefined variable: foo in hello.php on line 9

PHPLover
  • 1
  • 51
  • 158
  • 311
  • 1
    Explaination goes here in the first example [**unset()**](http://php.net/manual/en/function.unset.php) – Sahil Gulati Sep 15 '17 at 05:31
  • @SahilGulati: Whatever you want to say, please say it clearly and precisely. Don't be vague, it creates confusion only nothing else than that. – PHPLover Sep 15 '17 at 05:33
  • 1
    This is a great question, nice job minimizing an example. I'd like to mention though that globals should be avoided, and there are very few cases they should actually be used. They tend to make code behavior harder to decipher, as it will change based on the overall application state, instead of a local object state for example. – John V. Sep 15 '17 at 06:01

1 Answers1

2

The global keyword simply just import the variable into a function while $GLOBALS is PHP superglobal array.

An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.

Think of global keyword as clone/copy of the same variable while $GLOBALS is an actual storage where Global variables resides and you can add to or remove from the storage whenever you want.

So whenever a clone/copy of the variable is unset then It won't exists any more in that scope. e.x

<?php
function destroy_foo() 
{
    global $foo;
    unset($foo);
    echo $foo;-- Undefined variable: foo
}

$foo = 'bar';
destroy_foo();
?>

Hope it helps.

Basheer Kharoti
  • 4,202
  • 5
  • 24
  • 50
  • 1
    The difference between the local copied global and the fact that $GLOBALS is a direct access global is indeed the answer. However, this answer might be improved by explaining further how the difference changes the unset function's behavior. – John V. Sep 15 '17 at 05:49
  • 1
    Is it fine now @JohnV. ? – Basheer Kharoti Sep 15 '17 at 05:57