PREDEFINED VARIABLES



PHP provides large number of predefined and super global variables to running PHP script. Use of predefined variables we can track more information and they represent everything from external to built-in environmental variables.


What is superglobal Variable in PHP

Superglobal variable in PHP is scope variable to use all resources at global level. Many predefined variable is superglobal variable, the key of this array is name of global variable.


List of Predefined Variables in PHP

PHP $_GLOBAL

It is superglobal variable which is used to access outside variables from php script to inside local function in php.

In term of use:  $_GLOBALS [‘variable];

Example of global variable in PHP

Here in this example we are accessing $num variable from outside in local function.

<?php
						
function xyz()	//local function definition
{
	$num=10;	//local variable
	echo '$num in Global Scope: '. $_GLOBALS['num'];
	echo '$num in local scope:  '.$num;
}

$num=100;	//global variable for all local function
Xyz();		//calling local function

?>

PHP $_SERVER

It holds the information about headers, paths and script location using predefined variable in php.

In term of use:
$_SERVER[‘PHP_SELF’];     //get the name of running script

$_SERVER[‘SERVER_NAME’];     // get the server name

$_SERVER[‘HTTP_USER_AGENT’];     // get the information of client browser

PHP $_GET

It is associative array of global variables passed to the target script via URL parameters. The key of this associative array is the name of passed parameters from source script. It is restricted to send up to 1024 character only. After click on parameter based URL anyone can see parameter value in address bar along with URL.

In term of use
$_GET['parameter_name_passed_by_URL'];

Example of $_GET in PHP

Here we are explaining how we can send parameter via URL from source script to another target script. Most frequently used method in today’s development tradition


Source.php
<html>

<head<title>Source Page with URL</title></head>

<body>

<a href="target.php?a=100&b=200">Sending two parameters</a>

</body>

</html>


target.php
<?php
						
$a=$_GET['a'];
$b=$_GET['b'];

echo "Value of a=$a and b=$b";

?>

PHP $_POST

Post method is used sending information through FORM Tag using HTML controls. The post method transfer information via HTTP headers and its information are encoded. We can send large amount of data using $_POST.

In term of use
$_POST[‘html_control_name’];    //passing to target script

Example of $_POST in PHP

Here in example of calculator in php using $_POST.

CalculatorForm.php
<html>

<head<title>Calculator in PHP</title></head>

<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

Enter value First:<input type="text" name="a"/><br/>

Enter value Second:<input type="text" name="b"/><br/>

Operation:<select name="operation">

<option>Select</option>

<option>+</option>

<option>-</option>

<option>*</option>

<option>/</option>

</select><br/>

<input type="submit" name="submit" value="Calculation"/>

</form>

</body>

</html>


<?php
If ( $_SERVER['REQUEST_METHOD']=="POST" )
{
	$a=$_POST ['a'];
	$b=$_POST ['b'];
	$choice=$_POST ['operation'];
	Switch ( $choice )
	{
		Case '+':
			$c=$a+$b;
		Break;
		Case '-':
			$c=$a-$b;
		Break;
		Case '*':
			$c=$a*$b;
		Break;
		Case '/':
			$c=$a/$b;
		Break;
	Default:
		echo "Wrong Choice!";		
		exit();
	}
echo "<h2> Total=$c </h2>";
}

?>
Download here $_POST example in php

PHP $_FILE

$_FILE is superglobal variable, it can use to uploading a files.


PHP $_COOKIES

Cookie is very small file that server embeds on user’s computer to tracking them. Browser stores information sent by server. PHP provide set_cookies() function to set a cookies.


set_cookies in php

This function used to set cookies and require six parameters. We need to set this function in any script file above html tag.


In term of use:

Set_cookies(name, value, expire, path, domain, security); //parameter list

PHP $_SESSION

Session variable is special type of array that holds information for individual user again unique session id like sending information from one web page to another or many pages using unique identity by session id for individual user.


In term of use
$_SESSION['variable']=value;

PHP $_REQUEST

The variables in $_REQUEST are provided It is associative array that contents of $_GET, $_POST and $_COOKIES.


In term of use

If ( $_SERVER[‘REQUEST_METHOD’]=="POST" )
{
	//tracking whether form submitting or not.
}



PHP $_ENV

$_ENV is used to return the environment variables from the web server


Example of $_ENV in PHP

Here we are displaying username from environment variable.

 
<?php
						
$a=$_GET['a'];
$b=$_GET['b'];
echo "my usename is  ".$_ENV['username'];

?>