In this article, we will discuss $GLOBALS in PHP. $GLOBALS is a superglobal variable used to access global variables from anywhere in the PHP program. PHP stores all global variables in an array called $GLOBALS[index].
Syntax:
$GLOBALS['index']=value;
- value is the input value.
- The index is the unique key for a particular value.
Example: PHP Program to demonstrates $GLOBALS.
PHP
<?php
$GLOBALS [ 'A' ] = 100;
echo $A ;
echo "<br>" ;
$GLOBALS [ 'B' ] = "This is Global" ;
echo "\n" ;
echo $B ;
?>
|
Output:
100
This is Global
Example 2: PHP program to perform arithmetic operations using $GLOBALS.
PHP
<?php
$GLOBALS [ 'A' ] = 100;
$GLOBALS [ 'B' ] = 200;
echo $GLOBALS [ 'A' ]+ $GLOBALS [ 'B' ];
echo "<br>" ;
echo $GLOBALS [ 'A' ]- $GLOBALS [ 'B' ];
echo "<br>" ;
echo $GLOBALS [ 'A' ]* $GLOBALS [ 'B' ];
echo "<br>" ;
echo $GLOBALS [ 'B' ]/ $GLOBALS [ 'A' ];
echo "\n" ;
?>
|
Output:
300
-100
20000
2