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

PHP reads external variable $GLOBALS

不言
Release: 2023-03-25 16:32:01
Original
1930 people have browsed it

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
Copy after login

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
Copy after login

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
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template