Variable scope and global variables for PHP (text tutorial)

Source: Internet
Author: User
Tags variable scope
PHP as a script of the interpretation of the language, the characteristics of weak variables and the implementation of the release of resources of the characteristics of Zhucheng, PHP7 's strong join in the back-end language of the warlords set off a strong whirlwind. Well, because I usually do not pay attention to the scope of variables, thus writing this article is also a self-reminder.

HP as a script of the explanatory language, the characteristics of weak variables and the implementation of the release of resources of the characteristics of Zhucheng, PHP7 's strong addition is in the back-end language of the warlords set off a burst of strong whirlwind. Well, because I usually do not pay attention to the scope of variables, thus writing this article is also a self-reminder.

and PHP's syntax features and C + + also more like, plus $_post, $FILE and other global variables and construct () and destruct () and other magic variables make development more convenient.

But some people are not accustomed to the variable scope in PHP, PHP function variables and the global is completely isolated, that is, cannot access each other.

<?php    $test = ' Hello,world ';     ABC (); Nothing here output, because access is not $test variable    function abc () {            echo ($test);    }? >

Global and $globals[]

We can use the Global keyword to declare a variable, and the example above becomes

$test = ' Hello,world ';     ABC ();     Function abc () {        global $test;            echo $test;    }

The second way to access variables globally 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 ();

Originally thought that global and $globals in addition to the wording not the same, others are the same, but in the actual application found that 2 of the difference is still very big! See this example:

function Test1 () {     global $v 1, $v 2;     $v 2 =& $v 1; } function Test2 () {     $GLOBALS [' v3 '] =& $GLOBALS [' v1 '];} $v 1 = 1; $v 2 = $v 3 = 0; test1 (); Echo $v 2. " \ n "; Test2 (); echo $v 3. " \ n ";

Why is this:

Results

Shouldn't it be two five? We're looking at an example.

function test () {     global $a;     unset ($a); } $a = 1; Test (); echo $a;

Results

Clearly is unset, why still print out?
As is known to all, our function is always a private variable, unset really works, it unset a global value, and global in the function produces a pointer to the function external variable alias variable, rather than the real function external variable; $GLOBALS [ The actual call is an external variable, and the function will always be consistent inside and out!

Use ()

Everyone's understanding of use () is still used for namespaces, and PHP namespaces support two ways of using aliases or imports: using aliases for class names, or aliases for namespace names, which are implemented using operator use.
But what we're talking about today is this form: function use () {}
php5.3 New closure syntax,

Ordinary $a= "hello,world!"; $test = function () use ($a) {        echo $a;}; $test ();//Reference Object $ob= (object) Array (' name ' = ' GBW '); $test 2 = function () use ($ob) {    var_dump ($ob);}; $test 2 ();

The characteristics of the PHP closure is not much surprise, in fact, with the class can achieve similar or even much more powerful function, but also can not be compared with JS closure. So this is not a very common notation.

Related articles:

A trick is done, PHP in the closure function inside the use of the method and the difference, as well as the meaning of & reference for you to answer in detail

Learn more about PHP class, method keyword Tutorials

Detailed usage in PHP closure function () use ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.