What is Super global variable

A super global variable in PHP is a predefined  variable that is always  available   in all scopes throughout a script .  

Example a super global variable include 

          $_GET

    $_POST

3.           $_SESSION

4.           $_REQUEST

   $_COOKIE

   $_SERVER

7.          $_FILES

 1. $_GET

In PHP ,the $_GET variable is a super global variable that is used to collect data passed in the URL .It is an associative array that store data passed through  the get method in an http request .

Example :-

For  example ,if a user visits a website with  a url that looks like this :” showphp.php?filename=demo_global_get”;

2. $_POST 

In PHP, The $_POST variable is a super global variable that is used collect data passed in an HTTP request using the POST method .The POST method is typically used to submit data to the server ,such as when a user fills out a form on a website .

For example ,if a user fille out a form with fields for their name  and number and submit it ,the _POST variable would contain an array with keys  “name” and “number” and the value entered by  the user.

<form action =”<?php  echo $_SERVER[‘PHP_SELF’]; ?>” method=”post”>

<input type=”text” name =”name”>

<input type=”submit” > 

</form>

<?php  

If($_SERVER[“REQUEST_METHOD”]==$_POST)

{

$name=$_POST[‘name’];

If(empty($name))

{

Echo “Name is empty”;

}else{

    Echo $name;

}

}

 ?>

3. $_SESSION

In PHP ,The $_SESSION variable is a super global variable that is used to store data on the server that  is specific to  a user session .A session is a way to store information (in variable)

To be used across multiple page. When a user visits a website  a new session is created and a unique session ID is generated.

The $_SESSION variable is an associative array that stores data specific to a user`s session.

The data can be stored in the session  using the following syntax:

$_SESSION[‘key’]= value;

And then accessed using following syntax:-

$value=$_SESSION[‘key’];

When the user closed browser or otherwise ends the session ,the session data is destroyed.

Note:-  It is important to use session_start() function at the beginning  of  every page that you want to access session data.

4. $_COOKIE   

In PHP, the $_COOKIE variable is a super global variable that is used to store and retrieve data in the form of cookie. A cookie is a small piece of   data that is stored on the user`s computer by the web browser. Cookie are typically used to remember information        about a user ,such as their perferences  or login details.   

The $_Cookie variable is an associative array that stores data in the form of key-value pairs where the key is the name of the cookie and the value is the data stored in the cookie.

For example you can used the setcookie() function to create a new and store data in ,it and then access the data later using the  $_COOKIE variable.

For example ,to set a cookie named “username” with the value  “Ram”;

Setcookie(‘username’, ’Ram’, time()+(86400*30),”/”);

And then  you  can access the data like this

$username=$_COOKIE[‘username’];

Cookie are stored on the  user`s computer and     are sent back to the server with every request , so they can be used to persist data  across multiple page requests. It is important to keep in mind that cookies can be deleted by the user or expire after  a certain period of  time.  

5. $_SERVER

In PHP , the $_SERVER  variable is a super global variable that provides information about the server and the current script environment .it is an associative array that contains information such as the servers software, the current script file path , and information about the request such and IP address.

Same of the most commonly used element in the $_SERVER array include:-

·       $_SERVER[‘REQUEST_URL’] Which contains the URL of the current request.

·       $_SERVER[‘HTTP_HOST’] which contains the hostname of the server that the script is running on.

·       $_SERVER[‘REQUEST_METHOD’]  which contain the request method (GET ,POST, etc)

·       $_SERVER[‘REMOTE_ADDR’]  which contain IP address of the user making the request .

·       $_SERVER[‘SERVER_NAME’] Which contain the hostname

·       $_SERVER[‘SERVER_SOFTWARE’] Which contain s the server software version .

These variable can be useful in version scenarios such as logging create dynamic link, redirecting  and security purposes.

6. $_REQUEST

In PHP , the $_REQUEST variable is a super global variable that is used collect data from various sources , including the  $_GET ,$_POST and $_COOKIE variable .the $_REQUEST variable is an associative array that store data passed in  an HTTP request.

For example  , if a form uses the GET method to submit data will be stored in the $_GET variable ,but  it can also be accessed using the  $_REQUEST variable.

<form action=”name.php” method=”get”>

<input type=”text” name =”name”>

<input type=”submit” > 

</form>

IN this example  , the value entered in the form can be accessed using either $_GET[‘name’] or $_REQUEST[‘name’].

7. $_FILES

In PHP ,the $_FILES variable is a super global variable that is used to handle file uploads . the $_FILES variable is an associative array that information about the uploaded files, such as their names , types , and size.

When  a files is uploaded via            a form  ,the data is sent to the server in the form of a multipart  HTTP request . The $_FILES variable is used to access the  files data on the server side. The $_FILES array contains several element for each   files uploaded  ,such as:

$_FILES[‘file_field_name’][‘name’] which contain the original name of the file o the client`s computer

 $_FILES[‘file_field_name’][‘type’] which contains the MIME type of the file

$_FILES[‘file_field_name’][‘size’] which contains the size of the           file in bytes.

$_FILES[‘file_field_name’][‘tmp_name’] which contains the temporary name of the file store on the server

$_FILES[‘file_field_name’][‘error’] which contain the error code associated with the file upload.

For example 

$file_name=$_FILES[‘userfile’][‘name’];

$file_size=$_FILES[‘userfile’][‘size’];

$file_type=$_FILES[‘userfile’][‘type’];

$file_tmp_name=$_FILES[‘userfile’][‘tmp_name’];

$file_error =$_FILES[‘userfile’][‘error’];

It`s important to note that file uploads are subject to various security considerations such as file size  ,file type, and file name validation ,and it`s important to validate the file before storing it on the server.