SUPER GLOBAL VARIABLES OF PHP

In this tutorial you'll learn about PHP global variables

LET's START LEARNING



Super Global Variable : 
  •  Super Global Variable Means You can access it at any time without doing anything special.
  • That means they are pre-defined
  • You can access them in any function or class

THERE ARE MANY SUPER GLOBAL VARIABLES IN PHP AS FOLLOW :

  1. $GLOBALS
  2. $_SERVER
  3. $_REQUEST
  4. $_POST
  5. $_GET
  6. $_FILES
  7. $_ENV
  8. $_COOKIE
  9. $_SESSION

 

PHP $GLOBALS

  • $GLOBALS is one of the super global variable in PHP
  • It is used to access global variables from anywhere in the PHP script.
  • It is also used to access global variables from within functions or methods.
  • PHP stores all global variables in an array called $GLOBALS[index]. 
  • The index holds the name of the variable.

 Example :
<?php
$x = 75;
$y = 25;

function addition() {
     $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}

addition();
echo $z;
?>
 

Output :

100

PHP $_SERVER


  • This is PHP super global variable.
  • And the job of this variable is to holds information about headers, paths, and script locations.

Example :

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

Output :

/New folder/a.php
localhost
localhost
http://localhost/New%20folder/
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0
/New folder/a.php


PHP $_REQUEST

  • This variable is used to collect data after submitting an HTML form.
Example :

<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $name = $_REQUEST['fname'];
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo $name;
    }
}
?>

</body>
</html>

Output :

Before submit

After submit

PHP $_POST

  • The secure way to collect data after submitting a form is to used PHP $_POST Global variable with method="post"
  • $_POST is also widely used to pass variables.
Example :

<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $name = $_POST['fname'];
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo $name;
    }
}
?>

</body>
</html>
Output :
 Before submit
After submit

PHP $_GET

  • It is also be used to collect form data after submitting an HTML form with method="get".
  • $_GET can also collect data sent in the URL.

Example :

<!DOCTYPE html>
<html>
<body>

<a href="test_get.php?subject=PHP&web=aio-tutorials.blogspot.in">Test $GET</a>

</body>
</html>

Output :


 Before Click

After Click

 PHP $_FILES 

  • $_FILES one of the useful super global variable which is used to upload files. 
  • user can upload only that type of files which has defined by programmer (YOU)
Here is simple example to upload file using php  :

Select.html


Upload.php


PHP $_ENV

  • $_ENV is used to return the environment variables form the web server.
  1. <?php  
  2. echo $_ENV['username'];<br>  
  3. ?>  
Example :

<?php
echo "my username is ".$_ENV['username'];
?>

PHP $_COOKIE

  • Cookies loaded from a server to a client computer.
  • Aim to use Cookie is to storing some information regarding the client computer
  • So that when the same page from the server is visited by the user, necessary informations can be collected from the cookie itself, decreasing the latency to open the page
  • $_COOKIE retrieves those cookies.
Example :

<?php
setrawcookie();
print_r($_COOKIE);
?>

PHP $_SESSION

  • Sessions are wonderful ways to pass variables.
  • All you need to do is start a session by session_start();
  • Then all the variables you store within a $_SESSION, you can access it form anywhere in the server.
Example :

<?php
session_start();
$_SESSION['w3resource']='The largest online tutorial';
echo $_SESSION['w3resource'];
?>


IF YOU LIKE THIS TUTORIAL PLEASE SHARE AND SUPPORT - THANX

Comments