Super Globals

Scope is valid anywhere.

 

$GLOBALS

Associative array containing all global variables currently available within the global scope of the script, including ALL other super globals.

<?php
	echo "Here's the server name: " . $_SERVER['SERVER_NAME'] . "<br>";

	$serverGlobals = $GLOBALS['_SERVER'];

	echo "<br>Here's all the values of the super global _SERVER associative array:<br>" ;

	while (list($key,$value) = each($serverGlobals)) {

		print "$value<br>"; //display just the value

	}
?>
<?php
	$myFirst = 17;
	$mySecond = 42;

	function add() {

		$GLOBALS['total'] = $GLOBALS['myFirst'] + $GLOBALS['mySecond'];
	}
	add();

	echo $total;
?>

 

The Super Global variables are recognisable by their $_ prefix followed by the rest of their name in UPPERCASE

 

$_SERVER

Associative array containing all variables set by the web server, or other sources that directly related to the execution of the script.

<?php
	echo "<br>Here's the values of the super global $_SERVER associative array:<br>" ;

	while (list($key,$value) = each($_SERVER)) {

		print "$key = $value<br>";

	}
?>

 

 

$_GET

Contains all variables sent by HTTP GET request to the script via URL query string

 

IMPORTANT: Data sent with the GET method is limited and visible to everyone (i.e. displayed in the browser's address bar)

//A simple form to get name:
<html>
<body>
	<form action="get.php" method="get">
		Please enter your name: <input type="text" name="yourName" />
		<input type="submit" />
	</form>
</body>
</html>
//Resultant data passed in URL to get.php
<?php
	while (list($key,$value) = each($_GET)) {

		print "$key = $value<br>";

	}
?>

The top half of the above code asks the user to enter their name

Upon submit, the data is sent using the GET method to the file specified in the form action: "get.php"

The lower half of the above code (get.php) displays the data sent, which can be seen in it's URL

e.g. https://php.tech-academy.co.uk/get.php?yourName=DerrickĀ *(unsafe for sensitive data!)

The names of the form fields are used as the keys in the $_GET array

 

 

$_POST

Contains all variables sent via a HTTP POST request.

 

IMPORTANT: Data sent with the POST method is invisible and has no limits on the amount of data to send (default is 8MB, can be changed by post_max_size in php.ini)

//A simple form requesting users name:
<html>
<body>
	<form action="post.php" method="post">
		Please enter your name: <input type="text" name="yourName" />
		<input type="submit" />
	</form>
</body>
</html>
//Resultant data passed to post.php
<?php
	while (list($key,$value) = each($_POST)) {

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

The top half of the above code asks the user to enter their name

Upon submit, the data is sent using the POST method to the file specified in the form action: "post.php"

The lower half of the above code (post.php) displays the data sent, but cannot be seen in it's URL

e.g. https://php.tech-academy.co.uk/post.php

The names of the form fields are used as the keys in the $_POST array

 

 

$_COOKIE

Associative array containing all variables sent via HTTP cookies
//Assuming a cookie called "potato" has been previously set
<?php
	$expire=time()+60*60*24*7; //7 days

	setcookie("potato", "Cookie content goes here", $expire);
?>

//The contents of the cookie can be read
<?php
	echo $_COOKIE["potato"] ;
?>

 

 

$_FILES

Associative array containing all variables sent via HTTP POST file upload

//A simple form requesting users name:
<html>
<body>
	<form action="fileUpload.php" method="post" enctype="multipart/form-data">
		<input type="file" name="potato" />
		<input type="submit" name="submit" value="Submit" />
	</form>
</body>
</html>
//Clicking upon Submit button loads fileUpload.php
<?php
	while (list($key,$value) = each($_FILES["potato"])) { //display each key/value pair

		print "$key = $value";
	}

	move_uploaded_file($_FILES["potato"]["tmp_name"], "/uploads/" . $_FILES["potato"]["name"]);
	echo "Stored in: " . "/uploads/" . $_FILES["potato"]["name"] ;
?>

The top half of the above code provides user with choose file form, followed by Submit button

Upon submit, the data is sent using the POST method to the file specified in the form action: "post.php"

The lower half of the above code (fileUpload.php) displays the contents of the $_FILES array

Just for completeness, the last two lines copy the file from its temp location for permanent storage

 

 

$_ENV

Associative array containingĀ all environment variables set by your system or shell for the script

<?php
	while (list($key,$value) = each($_ENV)) {

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

*Probably quite dangerous to have this as a live script, since it shows a little too much information!

 

 

$_REQUEST

Associative array containing the contents of $_GET, $_POST and $_COOKIE

Beware: contains all variables from untrusted sources (e.g., site visitors), best to avoid unless really necessary

//A simple form requesting users name:
<html>
<body>
	<form action="request.php" method="post">
		Please enter your name: <input type="text" name="yourName" />
		<input type="submit" />
	</form>
</body>
</html>
//Resultant data passed in URL to request.php
<?php
	while (list($key,$value) = each($_REQUEST)) {

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

The top half of the above code asks the user to enter their name

Upon submit, the data is sent using the POST method (GET could alternatively have been used) to the file specified in the form action: "request.php"

The lower half of the above code (request.php) displays the data sent

The names of the form fields are used as the keys in the $_REQUEST array

 

 

$_SESSION

Contains all variables stored in a user's session

<?php
	session_start();

	$_SESSION['name'] = "Derrick";
	$_SESSION['live'] = "London";
	$_SESSION['age'] = 17;
	$_SESSION['bike'] = "Ducati";
	$_SESSION['band'] = "The Fall";

	while (list($key,$value) = each($_SESSION)) {

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

Leave a Reply