Php superglobal Variables Examples

In interview, people often ask what is super global variable in php, means which can be accessed from anywhere in the application.

Here you learn superglobal variable and global variable in php, what they are and how to use them in php application development.

Superglobals are built-in variables that are always available in all scopes. and we can access those variables from any function, class or file without having to do anything special.

Global and Superglobal variable in PHP

In PHP programming, we have Global and Super Global variables, Here is the list of PHP superglobal variables:

  • $GLOBALS
  • $_SERVER
  • $_REQUEST
  • $_FILES
  • $_ENV
  • $_COOKIE
  • $_SESSION
  • $_GET
  • $_POST
PHP $GLOBALS

Any $GLOBALS variable value can be accessed from any part of current page, even when you declare the variable within a function

In PHP application, all global variables values are stored in an array called GLOBALS $GLOBALS[index]. The index holds the name of the variable.

<?php 
function welcome() { 
    $GLOBALS['question1']="How are you today?";
}
welcome();
?>

Now we have written a global variable inside a function, now once the function is executed, we can directly use the variable anywhere in current page

We can access the GLOBAL variable values in following ways

<?php 
echo $question1;
echo $GLOBALS['question1'];
?>
PHP $_SERVER variables

Server $_SERVER is super global variable in PHP.

You can access any server variable like below

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

There are many server variables, here are some often used

  • Request method $_SERVER['REQUEST_METHOD']

    Returns the request method used in accessing the page (such as GET/POST)

  • Remote Host $_SERVER['REMOTE_HOST']

    Returns the Host name from where the user is viewing the page

  • Remote port $_SERVER['REMOTE_PORT']

    Returns the port is being used on the user's machine to communicate with the web server

PHP Request variables

PHP request $_REQUEST is used to collect data after submitting an HTML form.

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // collect data of form-input field
    $name = $_REQUEST['fullname'];
    if (empty($name)) {
        echo "Name is empty";
    } else {
        echo $name;
    }
}
?>
Form GET and POST

We can submit any form using two different methods, Get and Post, depending on what method is used in form, form data retrieving techniques will differ.

Here we have good tutorial about how form post and get method works

PHP $_COOKIE

A cookie is a small text file that is sent from server to client machine, In PHP, we can create and retrieve cookie value.

Learn how to implement Cookie in PHP web application

PHP $_SESSION

Session is superglobal variable; we can access it from any part of the PHP application, in PHP we can create Session variable, assign values, retrieve values and destroy or nullify them.

Learn how to implement Session in PHP web application

Here are some more key concepts about PHP programming you should know.

 
Php Superglobals
Learn php programming, php web development with free tutorials
Other Popular Tutorials
PHP Examples | Join PHP Online Course