Friday, October 24, 2025
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
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS