PHP Superglobals

PHP Superglobals were introduced in PHP 4.1 Version, All superglobals and are built-in variables and are always available in all the scopes.

PHP Superglobal variables are:

  • $GLOBALS
  • $_SERVER
  • $_REQUEST
  • $_POST
  • $_GET
  • $_FILES
  • $_COOKIE
  • $_SESSION

PHP $_SERVER

$_SERVER is a PHP super global variable which holds information about Server IP Address, Server Name, PHP File, PHP File Path, Request Method, Query String.

Superglobal Variables Description
$_SERVER['PHP_SELF'] Returns the current filename
$_SERVER['SERVER_ADDR'] Returns the IP address of the host server
$_SERVER['SERVER_NAME'] Returns the name of the host server (website name)
$_SERVER['SERVER_SOFTWARE'] Returns the server software (Apache)
$_SERVER['REQUEST_METHOD'] Returns the request method used to access the page (GET/POST)
$_SERVER['QUERY_STRING'] Returns the query string if the page is accessed via a query string
$_SERVER['REMOTE_ADDR'] Returns the IP address of the user computer
$_SERVER['SCRIPT_FILENAME'] Returns the absolute pathname (full path) of the current PHP file
$_SERVER['SCRIPT_NAME'] Returns the name of the current PHP file

PHP $_REQUEST

PHP $_REQUEST is used to collect data after submitting an HTML form.

Example 1

Example 2

PHP QUERY STRING

A Query string (also called an HTTP query string) is the set of characters automatically input in the address bar. The query string is a way of passing values with the URL(Uniform Resource Locator), the query string follows a separating character, a question mark (?), and the data to be passed.

Example 1: Query String

<a href="page2.php?value"> click </a>
<?php
	echo $_SERVER['QUERY_STRING'];
?>

Example 2: Query String with variable

<a href="page2.php?a=10"> click </a>
<?php
	echo $_GET['a'];
?>

Example 3: Query String with 2 variables.

<a href="page2.php?a=10&b=20"> click 
<?php
	echo $_GET['a'];
	echo $_GET['b'];
?>

Example 4: Query String with input variables.

<?php
	$x=15;
	$y=25;
?>
	<a href="page2.php?a=<?php echo urlencode($x);?>&b=<?php echo urlencode($y);?>"> click </a>
<?php
	echo $_GET['a'];
	echo $_GET['b'];
?>