What are PHP Global Variables and when do we use them?

PHP Global variables are very powerful and very useful variables while designing and developing webpages using PHP. It eases the process of data storing and sharing. Lets know about these PHP global variables.

PHP Global Variables

To understand php global variables lets revisit variables. In php, we use dollar sign at the beginning of a name and it becomes a variable name. Unlike Java, .Net, C# we do not need keyword to define the type of variable name. And PHP engine can understand the type of variable from the value assigned to it. 

Okay, now, variable has following limitations. 

  • User defined variables are only available to the webpage where it is defined. 
In some situation, we may want to access a variable value from any of the webpage. So what could be the solution? To fullfill such requirement, PHP has global variables. The best part about global variables, they are available to all the webpages inside a server. 

Example:

A user has logged in. We may want to show his name in all the webpages. In such case Session Global variables are useful. 

How many types of Global Variables available in PHP?

PHP has 8 types of Global Variables.  These global variables are defined by PHP. And following PHP Global variables are available through out any webpage in a website. 

$_SERVER

$_SERVER is a PHP defined global variable and it contains information about the server information of visited user. 

Example:

If you want to know what was the referring webpage from where user visited your website, then you need to call this global variable. ($_SERVER['HTTP_REFERER'])

$_REQUEST

$_REQUEST is another PHP defined global variable.It contains the data processed through post method or get method

Example:

Login Procedure.

After user fills the login form, data is send to the php script that checks whether the provided information are valid or not. In that login validation script, can get the user provided information using $_REQUEST. 

<form method="post" action="login-validation.php">
  User Email: <input type="text" name="uemail">
  User Password: <input type="password" name="upwd">
  <input type="submit">
</form>
Inside the login-validation script:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $user_email = $_REQUEST['uemail'];
    $user_pwd=$_REQUEST['upwd'];
    echo $user_email." ".$user_pwd;
}
?>


$_POST

$_POST is a PHP defined global variable and it contains the values that is sent through post method.

<form method="post" action="login-validation.php">
  User Email: <input type="text" name="uemail">
  User Password: <input type="password" name="upwd">
  <input type="submit">
</form>
Inside the login-validation script:

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect value of input field
    $user_email = $_POST['uemail'];
    $user_pwd=$_POST['upwd'];
    echo $user_email." ".$user_pwd;
}
?>


$_GET

$_GET is a PHP defined global variable and it contains the data, that is sent over GET method.

<form method="get" action="login-validation.php">
  User Email: <input type="text" name="uemail">
  User Password: <input type="password" name="upwd">
  <input type="submit">
</form>
Inside the login-validation script:

<?php
if ($_SERVER["REQUEST_METHOD"] == "GET") {
    // collect value of input field
    $user_email = $_GET['uemail'];
    $user_pwd=$_GET['upwd'];
    echo $user_email." ".$user_pwd;
}
?>


$_FILES

$_FILES is another PHP global variable. When we upload a file such as image, video or doc file then this global variable contains the file.

$_COOKIE

If we want to know a user has visited certain webpages then one solution is to store in the database. But this approach is going to cost the consumption of resources available in the hosting server. Therefore there is alternate solution is to store these data in users system. We store it using $_COOKIE php global variable.

$_SESSION

Alike $_COOKIE global variable, $_SESSION stores data in user's system. For example, if we have checked, the user who provided login credentials is a valid user, then we must store about it in a way. So in such case $_SESSION are the php defined global variables can be used to store user login status, users email etc. 
What are PHP Global Variables and when do we use them? What are PHP Global Variables and when do we use them? Reviewed by Pixel Stream on December 09, 2019 Rating: 5

No comments:

Powered by Blogger.