8

In PHP I want to know the differences between the GLOBAL and GLOBALS.

Some example:

print_r($GLOBALS);
hakre
  • 193,403
  • 52
  • 435
  • 836
balaphp
  • 1,306
  • 5
  • 20
  • 40
  • `$GLOBALS` is an array, `global` is a PHP keyword. They essentially do the same thing but in a different way – Pekka Nov 07 '11 at 10:47
  • but why we cant access the session and cookie variables by using $GLOBAL? i can access the variables which i have created using $GLOBAL. – balaphp Nov 07 '11 at 10:50
  • 3
    Because that's not what $GLOBALS is there for. $GLOBALS is for variables defined in the global scope, no more no less. That's just the way it is. – Pekka Nov 07 '11 at 10:50
  • See also: [Is there any differents between $GLOBALS\[“test”\] and global $test?](http://stackoverflow.com/questions/2888013/is-there-any-differents-between-globalstest-and-global-test) – hakre Nov 24 '12 at 20:15

5 Answers5

15

That are two different things related to the same: global variables.

$GLOBALS - PHP superglobal array representing the global variable table accessible as an array. Because it's a superglobal, it's available everywhere.

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.

global - Keyword to import a specific global variable into the local variable table.


Then you asked:

But why we cant access the session and cookie variables by using $GLOBALS?

That's wrong, you can access session and cookie variables by using $GLOBALS:

$GLOBALS['_SESSION']['session_variable_name']

However $_SESSION is a superglobal as well, so you don't need to use either $GLOBALS nor global to access session variables from everywhere:

$_SESSION['session_variable_name']

Same applies to $_COOKIE.

hakre
  • 193,403
  • 52
  • 435
  • 836
6

They are two different things.

global is a keyword which tells that the variable is from a global scope. E.g. if you're about to access a variable inside a function that's defined outside you'll need to use the global keyword to make it accessible in the function.

$GLOBALS is a superglobal array. Superglobal simply means that it is available in all scopes throughout a script without the need of using the global keyword.

Marcus
  • 12,296
  • 5
  • 48
  • 66
2

$GLOBALS is an array and global is a keyword to declare or use global variables

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223
1

I think your confusion in in between $GLOBAL and $GLOBALS.

$GLOBALS is a superglobal array that it is available in all scopes throughout a script without the need of using the global keyword.

You are trying to access the session and cookie variables by using $GLOBAL and that's wrong. Please use $GLOBALS instead. $GLOBAL is nothing.

But global is a keyword which tells that the variable is from a global scope.

1

$GLOBALS : 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

GLOBAL/global is a keyword for setting a variable global.

References :

http://php.net/GLOBALS

http://php.net/global

Mob
  • 10,958
  • 6
  • 41
  • 58