PHP Superglobal - $GLOBALS (With Examples)

By Kongnyu Carine, Shahnail Khan Last updated : December 14, 2023

PHP $GLOBALS

PHP $GLOBALS is the only superglobal that does not begin with an underscore (_). It is an array that stores all the global scope variables.

$GLOBALS is a PHP superglobal variable that provides access to all global variables in a script. These global variables include those declared outside functions or classes, making them accessible from anywhere within the script.

$GLOBALS is used to access all global variables (variables from global scope) i.e. the variable that can be accessed from any scope in a PHP script.

Syntax of PHP $GLOBALS

Here's the syntax for using the $GLOBALS superglobal in PHP:

<?php
$variableName = "some value"; // Declare a global variable

function myFunction()
{
    // Access the global variable using $GLOBALS
    echo $GLOBALS["variableName"];
}

// Call the function to see the output
myFunction();
?>

Examples of PHP $GLOBALS

We will see how to access a variable defined globally with the $GLOBALS superglobal?

Example 1

Accessing a Global Variable

<?php
//global variable
$name = "my name is sam";

//function
function sayName()
{
    echo $GLOBALS["name"];
}

//calling the function
sayName();
?>

Output

my name is sam

By defining the $name variable it automatically is stored in the superglobal variable $GLOBALS array. This explains why we can access it in the sayName() function without defining it in the function.

Example 2

PHP code to find sum of two numbers by accessing global variables using $GLOBALS

<?php
//global variables
$num1 = 36;
$num2 = 24;

//function to access global variables
function add2Numbers()
{
    $GLOBALS["sum"] = $GLOBALS["num1"] + $GLOBALS["num2"];
}

//calling function
add2Numbers();

//printing sum using global variable
echo $sum;
?>

Output

60

In the code above, num1 and num2 are global variables so we are accessing them using $GLOBALS, and sum is a variable present within $GLOBALS, thus, it is accessible from outside the function also.

Example 3

Using $GLOBALS Within a Function

<?php
// Global variable
$outsideVar = "I am outside the function.";

// Function definition
function displayOutsideVar()
{
    echo $GLOBALS["outsideVar"];
}

// Calling function
displayOutsideVar();
?>

Output

I am outside the function.

Here, there's a variable $outsideVar declared outside a function. Inside a function, $GLOBALS['outsideVar'] is used to access and display its value.

Example 4

Modifying a Global Variable Within a Function

<?php
// Global variable
$counter = 0;

// Function
function incrementCounter()
{
    $GLOBALS["counter"]++;
}

// Calling function
incrementCounter();

// Printing global variable after
// modifying through function
echo $counter;
?>

Output

1

In this example, a counter variable is declared globally. Inside a function, $GLOBALS['counter'] is used to increment its value.

Example 5

Dynamic Variable Naming

<?php
// Global variable
$dynamicVarName = "myDynamicVar";

// Dynamic variable naming
$$dynamicVarName = "I am dynamically accessed!";

// Printing
echo $GLOBALS["myDynamicVar"];
?>

Output

I am dynamically accessed!

In this code, we have a dynamic variable name stored in another variable, like $dynamicVarName. By using double dollar signs $$, we can create a variable with the dynamic name and assign a value to it.

These examples show us how $GLOBALS helps in accessing and manipulating global variables, making it easier to work with data across different parts of your PHP script.



All PHP Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.