Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

$GLOBALS

This super-global is a PHP predefined variable, which holds a reference to every global variable in the application. It behaves as an associative array, where each key is the name of a global variable and each value is a reference to that variable, so reading or writing $GLOBALS['x'] inside a function is equivalent to declaring global $x; first. Because it bypasses normal variable scoping, relying on $GLOBALS makes code harder to test and reason about, and it is generally considered a code smell in modern PHP, where dependency injection or object properties are preferred. Since PHP 8.1, some indirect writes to $GLOBALS – through list assignment, by-reference foreach, or array functions – are restricted, since they no longer directly modify the underlying global variable.

<?php

    // implicit global variable
    $aGlobalVariable = 1; 
    
    function trumpet() {
        echo $GLOBALS['aGlobalVariable'];
        $GLOBALS['aGlobalVariable'] = 2;
    }

    trumpet(); // displays 1
    echo aGlobalVariable; // 2

?>

Documentation

See Also