Global vs Suparglobals in PHP

The range of the availability or scope of variables are called global of superglobals.

Global

A variable declared can be global using the keyword global. Placing global keyword in the front of a variable change it to as global scope. No it is accessible within a function or within a include file if it is not within file of function. Placing 'global' keyword in front of the and already existing variable tells PHP to use the variable having the name.



<?php
// DEFINE A VARIBLE NAMED $one
$one = 'ONE';
// DEFINE A ANOTHERE VARIABLE NAME $two
$two = 'TWO';

// CREATE A FUNCTION WIT HTHE NAME OF THEST
function test()
{
// USING GLOBAL KEYWORD FIND THE BOTH VARIABLES AVAILBLE IN THIS PROGRAM
global $one, $two;

// PRINT A STRING USIGN THE GLOBAL SCOPE VARIABLE CONCATED WITH THE STRING
echo "Red function has " . $one . " and Green function has " . $two . " Parameters";

}
?>

Superglobals

Superglobals are PHP built in variables or members That have already have global scope they don't have need to add global keyword. There are some superglobals are listed below.




$GLOBALS, $_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION, $_REQUEST, $_ENV

Students Tech Life