PHP Superglobal – $GLOBALS

PHP $GLOBALS

$GLOBALS is a PHP very worldwide variable which is utilized to get to worldwide factors from anyplace in the PHP content (additionally from inside capacities or strategies). 

PHP stores every worldwide variable in a cluster called $GLOBALS[index]. The file holds the name of the variable.

Example #1 Using global

<?php
$a = 1;
$b = 2;

function Sum()
{
    global $a, $b;
    $b = $a + $b;
} 

Sum();
echo $b;
?>

Example #2 Using $GLOBALS instead of global

<?php
$a = 75;
$b = 25;

function add() {
    $GLOBALS['c'] = $GLOBALS['a'] + $GLOBALS['b'];
}

add();
echo $c;
?>

Leave a comment

Your email address will not be published. Required fields are marked *