Home  >  Article  >  Backend Development  >  Why are $GLOBAL[''] and the keyword global affected by static variables in PHP?

Why are $GLOBAL[''] and the keyword global affected by static variables in PHP?

WBOY
WBOYOriginal
2016-12-01 00:01:22941browse

Reply content:

First time encountering this problem. I'm a little curious, how did the questioner discover this problem?

Let me tell you my understanding.

First we need to clarify the concept of "assignment".

When $a = 1;, it does not mean adding a data whose name is $a and whose value is 1.
When $b = &$a;, it doesn’t mean pointing $b to $a or vice versa.

This process is like this:
<code class="language-php"><span class="x">$a = 1;</span>
</code>
Read the PHP manual, there are many examples
PHP: Variable scope
PHP: Explanation of references
PHP: Pass by reference
PHP: What does a reference do



Variable scope

The scope of a variable is the context in which it is defined (that is, its effective scope). Most of the PHP variables have only a single scope. This single range span also contains Files introduced by include and require. For example:


<code class="language-php"><span class="cp"><?php</span>
<span class="nv">$a</span> <span class="o">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="k">include</span> <span class="s1">'b.inc'</span><span class="p">;</span><span class="cp">?></span><span class="x"></span>
</code>
<code class="language-text">global $a;
时会 新建 一个指向与外部$a同地址的指针

$GLOBALS['a'] = &$static_a;
时会 把外部$a 指针 指向 $static_a的内容,抛弃原先的指向
</code>
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