PHP 7 Superglobal Variables

PHP does not support global variables, certain internal variables treat like global variables. Those variables are called Superglobal variables.

These are the following superglobal variables.

$_GET

GET method converts the name/value of form data in query string and append to the requested page.

Example -

https://etutorialspoint.com?name=john&age=32

$_GET[] array retrieve variables from the query string or URL.
Like - $_GET['name'] contains value 'john' and $_GET['age'] contains value 32.

As GET method is visible to everyone in the url so this is not secure and not preferred to send sensitive information.


$_POST

POST method embedded the form name/value data and send in HTTP headers. So it is not visible in the requested url and more secure than GET method. The $_POST[] array retrieves all the sent information.

Example -

$_POST['name'] and $_POST['age'].




$_COOKIE

Cookie is used to make the data persistent across request, means we can use the data store in cookie across different pages.
setcookie() function is used to set the data and store in the browser.

Example -

setcookie('username', $username, time()+20000, '/');

In the above line username is the name of cookie to be set, $username has the value of the username cookie and time()+20000 sets the expiration time.


$_SESSION

A PHP session stores data on the server rather than user's computer.
In PHP, the session_start() function is used to create a client session and generates a session id.

Example -

<?php 
session_start();
$_SESSION['userid'] = '122';
$_SESSION['department'] = 'software';
?>

$_SERVER

It stores the information about web server and execution environment. The entries in this array are created by the web server.

Example -

<?php 
echo $_SERVER['SERVER_NAME']."<br>";
echo $_SERVER['HTTP_HOST']."<br>";
echo $_SERVER['PHP_SELF']."<br>";
?>

Output -

www.etutorialspoint.com
www.etutorialspoint.com
/index.php





Read more articles


General Knowledge



Learn Popular Language