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

global Scope

The global scope is the top level scope. It is the scope when no function, or method scope is active. It is also the first scope when starting execution.

Variables from the global scope are accessible in other scopes by using the global keyword, or the $GLOBALS variable. Variables in the global scope are automatically added to the $GLOBALS variable; in local scopes, the global keyword has that effect too.

Some special variables are called superglobal: they are available in every scope.

The opposite of the global scope is the local scope.

<?php

    $trunk = 'A';

    function counter() {
        echo $GLOBALS['A'];

        global $trunk;

        echo $trunk;
    }
    
    counter();

?>

Documentation

See Also