Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

What is the use of $GLOBALS in PHP ?

  • Last Updated : 18 Oct, 2021

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 
  
// Initialize an index A
// and set to 100
$GLOBALS['A'] = 100;
  
// Display
echo $A;
echo "<br>";
  
// Initialize an index A
// and set to a string
$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 
  
// Initialize an A index and set to 100
$GLOBALS['A'] = 100;
    
// Initialize an B index and set to 200
$GLOBALS['B'] = 200;
  
// Display addition
echo $GLOBALS['A']+$GLOBALS['B'];
echo "<br>";
  
// Display subtraction
echo $GLOBALS['A']-$GLOBALS['B'];
echo "<br>";
  
// Display multiplication
echo $GLOBALS['A']*$GLOBALS['B'];
echo "<br>";
  
// Display division
echo $GLOBALS['B']/$GLOBALS['A'];
echo "\n";
  
?>

Output:

 300
-100
 20000
 2


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!