Super Global Variables

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP $GLOBALS (super global) variable

Last update on January 25 2017 11:38:36 (UTC/GMT +8 hours)


$GLOBAL is a php super global variable which can be used instead of 'global' keyword to access variables from global scope, i.e. the variables which can be accessed from anywhere in a php script even within functions or methods.

Here is an example to testify the theory mentioned above:  

view plaincopy to clipboardprint?

  1. $s = 25;  

  2. $t = 50;  

  3. function subtraction()  

  4. {  

  5. $GLOBALS['v'] = $GLOBALS['t'] - $GLOBALS['s'];  

  6. }   

  7. subtraction();  

  8. echo $v;  

  9. ?>   

In the code above, since v is a variable present within $GLOBALS array, it is accessible form outside the function also.

View the example of $GLOBALS variable in the browser