Wednesday, June 17, 2026
HomeLanguagesWhat is the use of $GLOBALS in PHP ?

What is the use of $GLOBALS in PHP ?

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
RELATED ARTICLES

Most Popular

Dominic
32516 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6898 POSTS0 COMMENTS
Nicole Veronica
12014 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12109 POSTS0 COMMENTS
Shaida Kate Naidoo
7019 POSTS0 COMMENTS
Ted Musemwa
7262 POSTS0 COMMENTS
Thapelo Manthata
6976 POSTS0 COMMENTS
Umr Jansen
6964 POSTS0 COMMENTS