Home  >  Article  >  Backend Development  >  PHP reads external variable $GLOBALS

PHP reads external variable $GLOBALS

不言
不言Original
2018-05-08 13:59:121864browse

This article mainly introduces about PHP reading external variables $GLOBALS, which has certain reference value. Now I share it with everyone. Friends in need can refer to it

Reading external variables $GLOBALS

$one = 1;function demo(){
    $two = 2;    // 如果直接 $one 会在代码中报错
    // 读取自定义函数demo外部的变量one - $GLOBALS['one']
    $result = $two + $GLOBALS['one'];    
    return $result;
}echo demo(); // 3

Create an external variable

function demo2(){
    // 创建一个外部变量
    $GLOBALS['aaa'] = 'aaaaaaaa';    
    echo &#39;demo2&#39; . &#39;<br/>&#39;;
}// 这样用会报错, 需要先调用demo2函数才行
//echo $aaa;demo2();
 // demo2echo $aaa . &#39;<br/>&#39;; 
 // aaaaaaaa

Call multiple external variables

$one = 1;$two = 2;$five = 5;function demo(){
    // 调用多个外部变量
    global $one, $two, $five;    
    return $one + $two + $five;
}echo demo() . &#39;<br/>&#39;; // 8

Related recommendations:

Php reads data Basic operations

php method of reading the contents of BT seed files

The above is the detailed content of PHP reads external variable $GLOBALS. 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