Php Super Global Objects

Php has the following Super Global Objects.
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION

Let us go ahead and understand them one by one with proper examples.

$GLOBALS is used for accessing global variables in a page directly and from inside functions.
For instance.


<?php
var_dump($GLOBALS);
?>



Global variables can be accessed inside functions by using $GLOBALS['varname'];

while variables inside functions can be made global by declaring as $GLOBALS['varname']=value.

&nbsp;

Every variable in a page in php goes into the $GLOBALS collection. Thus the functions isset, unset,count and other collection functions can be used.

Let us print a dump of the $GLOBALS variable.
<pre>

array (size=8)
'_GET' =>
array (size=0)
empty
'_POST' =>
array (size=0)
empty
'_COOKIE' =>
array (size=0)
empty
'_SERVER' =>
array (size=36)
'HTTP_HOST' => string 'localhost' (length=9)
'HTTP_CONNECTION' => string 'keep-alive' (length=10)
'HTTP_CACHE_CONTROL' => string 'max-age=0' (length=9)
'HTTP_UPGRADE_INSECURE_REQUESTS' => string '1' (length=1)
'HTTP_USER_AGENT' => string 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36' (length=114)
'HTTP_DNT' => string '1' (length=1)
'HTTP_ACCEPT' => string 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8' (length=85)
'HTTP_ACCEPT_ENCODING' => string 'gzip, deflate, br' (length=17)
'HTTP_ACCEPT_LANGUAGE' => string 'en-US,en;q=0.9' (length=14)
'PATH' => string 'C:\Python27\;C:\Python27\Scripts;C:\oraclexe\app\oracle\product\10.2.0\server\bin;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program File'... (length=1206)
'SystemRoot' => string 'C:\Windows' (length=10)
'COMSPEC' => string 'C:\Windows\system32\cmd.exe' (length=27)
'PATHEXT' => string '.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC' (length=53)
'WINDIR' => string 'C:\Windows' (length=10)
'SERVER_SIGNATURE' => string '<address>Apache/2.4.37 (Win64) PHP/7.2.14 Server at localhost Port 80</address>
' (length=80)
'SERVER_SOFTWARE' => string 'Apache/2.4.37 (Win64) PHP/7.2.14' (length=32)
'SERVER_NAME' => string 'localhost' (length=9)
'SERVER_ADDR' => string '::1' (length=3)
'SERVER_PORT' => string '80' (length=2)
'REMOTE_ADDR' => string '::1' (length=3)
'DOCUMENT_ROOT' => string 'C:/wamp/www' (length=11)
'REQUEST_SCHEME' => string 'http' (length=4)
'CONTEXT_PREFIX' => string '' (length=0)
'CONTEXT_DOCUMENT_ROOT' => string 'C:/wamp/www' (length=11)
'SERVER_ADMIN' => string 'wampserver@wampserver.invalid' (length=29)
'SCRIPT_FILENAME' => string 'C:/wamp/www/globals/globalone.php' (length=33)
'REMOTE_PORT' => string '4535' (length=4)
'GATEWAY_INTERFACE' => string 'CGI/1.1' (length=7)
'SERVER_PROTOCOL' => string 'HTTP/1.1' (length=8)
'REQUEST_METHOD' => string 'GET' (length=3)
'QUERY_STRING' => string '' (length=0)
'REQUEST_URI' => string '/globals/globalone.php' (length=22)
'SCRIPT_NAME' => string '/globals/globalone.php' (length=22)
'PHP_SELF' => string '/globals/globalone.php' (length=22)
'REQUEST_TIME_FLOAT' => float 1552454155.967
'REQUEST_TIME' => int 1552454155
'_ENV' =>
array (size=0)
empty
'_REQUEST' =>
array (size=0)
empty
'_FILES' =>
array (size=0)
empty
'GLOBALS' =>
&array<

What happens if I add a a new variable x


<?php
$x=10;
var_dump($GLOBALS);
?>

 

The following lines get added at the end.


'GLOBALS' =>
&array< 'x' => int 10

To get a list of all variables in the $GLOBALS collection use.


<?php
$x=10;
$y=10;
$keys=array_keys($GLOBALS);
$n=count($keys);
for($i=0;$i<=$n-1;$i++)
{
$key=$keys[$i];
$value=$GLOBALS[$key];

print( "$key=$value<br/>");

?>

The last entries showing the variables are printed correctly. The rest get string conversion errors. Let us suppress the errors.


<?php
$x=10;
$y=10;
error_reporting(0);
$keys=array_keys($GLOBALS);
$n=count($keys);
for($i=0;$i<=$n-1;$i++)
{
$key=$keys[$i];
$value=$GLOBALS[$key];

print( "$key=$value<br/>");
}
?>


_GET=Array
_POST=Array
_COOKIE=Array
_SERVER=Array
_ENV=Array
_REQUEST=Array
_FILES=Array
GLOBALS=Array
x=10
y=10

 

For isset, unset please read this page.


<?php
$x=10;
if(isset($x))
print("X=$x<br/>");
else
print ("X not found<br/>");

if(isset($y))
print("Y=$y<br/>");
else
print ("Y not found<br/>");
$y=15;

&nbsp;

if(isset($x))
print("X=$x<br/>");
else
print ("X not found<br/>");

if(isset($y))
print("Y=$y<br/>");
else
print ("Y not found<br/>");
$y=15;

unset($x);
unset($GLOBALS['y']);
if(isset($x))
print("X=$x<br/>");
else
print ("X not found<br/>");

if(isset($y))
print("Y=$y<br/>");
else
print ("Y not found<br/>");
?>

 

 

Leave a Reply