Home  >  Article  >  Backend Development  >  What is the usage of $GLOBALS in php

What is the usage of $GLOBALS in php

王林
王林Original
2021-06-09 17:30:423021browse

The usage of $GLOBALS in php is to reference all variables available in the global scope, such as [$GLOBALS["foo"]]. $GLOBALS is a global combined array containing all variables.

What is the usage of $GLOBALS in php

The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.

$GLOBALS is used to reference all variables available in the global scope. It is a global combined array containing all variables. The name of the variable is the key of the array.

Example:

<?php
function test() {
    $foo = "local variable";

    echo &#39;$foo in global scope: &#39; . $GLOBALS["foo"] . "\n";
    echo &#39;$foo in current scope: &#39; . $foo . "\n";
}

$foo = "Example content";
test();
?>

The output of the above routine is similar to:

$foo in global scope: Example content
$foo in current scope: local variable

Related video sharing: php video tutorial

The above is the detailed content of What is the usage of $GLOBALS in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn