Home > Article > Backend Development > PHP variable scope and global variables (graphic tutorial)
As a script interpreted language, PHP has the characteristics of weak variables and the characteristics of releasing resources after execution. The strong addition of PHP7 has set off a strong whirlwind among the back-end languages. . Well, since I usually don’t pay much attention to the scope of variables, writing this article can be regarded as a self-reminder.
HP is an interpreted language for scripts. It has the characteristics of weak variables and the ability to release resources after execution. The strong addition of PHP7 has caused a stir among the back-end languages. A strong whirlwind. Well, since I usually don’t pay much attention to the scope of variables, writing this article can be regarded as a self-reminder.
The grammatical features of PHP are similar to those of C. In addition, global variables such as $_POST and $FILE and magic variables such as construct() and destruct() make development more convenient.
But some people are not used to the variable scope in PHP. In PHP, function variables and the global world are completely isolated, that is, they cannot access each other.
<?php $test = 'hello,world'; abc(); //这里什么都不输出,因为访问不到$test变量 function abc(){ echo($test); }?>
global and $GLOBALS[]
We can use the global keyword to declare variables. The above example becomes like this
$test = 'hello,world'; abc(); function abc(){ global $test; echo $test; }
That’s it. The second way to access variables in the global scope is to use a special PHP custom $GLOBALS array. The previous example can be written as:
$test = 'hello,world'; function abc(){ echo $GLOBALS['test']; } abc();
I originally thought that global and $GLOBALS were the same except for the different writing methods. However, in practical applications, I found that the difference between the two is still very big! Look at this example:
function test1() { global $v1, $v2; $v2 =& $v1; } function test2() { $GLOBALS['v3'] =& $GLOBALS['v1']; } $v1 = 1; $v2 = $v3 = 0; test1(); echo $v2 ."\n"; test2(); echo $v3 ."\n";
Why is this:
The result
shouldn’t be two fives? We are looking at an example
function test() { global $a; unset($a); } $a = 1; test(); echo $a;
The result
is obviously unset, why is it still printed?
As we all know, our function is always a private variable. Unset does work. It unsets a global value, and global generates an alias variable in the function that points to the external variable of the function, not the real function. External variables; $GLOBALS[] is indeed called an external variable, and it will always be consistent inside and outside the function!
use()
Does everyone’s understanding of use() still refer to the use of namespace? PHP namespace supports two ways of using aliases or imports: Use aliases for class names, or use aliases for namespace names. Aliases are implemented through the operatoruse.
But what we are talking about today is this form: function use(){}
php5.3New closure syntax,
//普通 $a="hello,world!";$test = function () use($a){ echo $a; }; $test();//引用 对象 $ob=(object)array('name' => 'gbw');$test2 = function () use($ob){ var_dump($ob); };$test2();
PHP closure features are not There is no big surprise. In fact, similar or even more powerful functions can be achieved by using CLASS, which is not comparable to js closures. So this way of writing is not very common.
Related articles:
Detailed explanation of PHP class and method keyword tutorial
Detailed usage method in PHP closure function() use()
The above is the detailed content of PHP variable scope and global variables (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!