Wednesday, July 29, 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

1 COMMENT

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6903 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6981 POSTS0 COMMENTS
Umr Jansen
6973 POSTS0 COMMENTS