Home > Backend Development > PHP Tutorial > PHP variable scope and global variables (graphic tutorial)

PHP variable scope and global variables (graphic tutorial)

亚连
Release: 2023-03-26 10:38:02
Original
1849 people have browsed it

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 = &#39;hello,world&#39;;
     abc(); //这里什么都不输出,因为访问不到$test变量
    function abc(){            echo($test);
    }?>
Copy after login

global and $GLOBALS[]

We can use the global keyword to declare variables. The above example becomes like this

$test = &#39;hello,world&#39;;
     abc(); 
    function abc(){        global $test;            echo $test;
    }
Copy after login

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 = &#39;hello,world&#39;;    function abc(){        echo $GLOBALS[&#39;test&#39;];
    }
    abc();
Copy after login

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[&#39;v3&#39;] =& $GLOBALS[&#39;v1&#39;]; 
} 
$v1 = 1; 
$v2 = $v3 = 0; 
test1(); 
echo $v2 ."\n"; 
test2(); 
echo $v3 ."\n";
Copy after login

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

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(&#39;name&#39; => &#39;gbw&#39;);$test2 = function () use($ob){
    var_dump($ob);
};$test2();
Copy after login

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:

Get it all in one move, the usage and difference of use in the closure function in PHP, and the meaning of & reference will be answered in detail for you

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!

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