Why is there an invalid or incorrect global variable?

Source: Internet
Author: User
Before I introduce the invalid global variable in PHP, I'll give you a brief introduction to what is the PHP global variable global? First, the Phpglobal scope is not applied to the entire Web site, but applies to the current page, including all files of include or require. However, the global variable defined in the function body can be used within the function body, and the global variable defined outside the function body cannot be used within the function body.

Incidentally, the PHP $GLOBALS variable refers to all the variables available in the global scope, in $GLOBALS array, each variable is an element, the key name corresponds to the variable name, and the value corresponds to the contents of the variable. $GLOBALS exists globally because $GLOBALS is a hyper-global variable. Note the $globals, such as the variable $a1, written as $globals[' A1 '.

So the following is a shallow and easy to understand examples to explain in detail the global in PHP invalid problem:

First demo1.php the page code as follows:

<?php    $site _name = ' codeplayer ';    function Sayhi () {        global $site _name;        echo "hello! Welcome to $site _name! ";    }    ? >

The demo2.php page code is as follows:

<?php    function Include_view_page () {        include ' demo2.php ';        Sayhi ();                        }    Include_view_page ();    ? >

When we visit the demo1.php page, we can display the welcome statement correctly. However, when we use the browser to access the demo2.php page the output is as follows:

Hello! Welcome to!

When we call the Sayhi () function in function include_view_page (), the $site_name of global in the demo2.php page Sayhi () function is not recognized correctly and takes effect.

In fact, when we include demo2.php pages in function Include_view_page (), the variable $site_name of the demo2.php page is equivalent to the scope stored within the Include_view_page () function. We all know that within a function global a variable actually establishes a reference to the page global variable within the function. In this case, the $site _name variable is only a local variable within the Include_view_page () function for demo1.php, so the global variable cannot be displayed, and we cannot get the correct variable and variable value when we make the related call.

In PHP, we especially need to be aware of the problem of having a page in a function like the one above that causes the scope of variables in that page to change. To avoid this situation, we should try to minimize the multiple-level include call and try not to use include within the function. In addition, we can declare $site_name in the form of a global variable on the demo2.php page.

demo2.php    <?php    global $site _name;    $site _name = ' codeplayer ';    function Sayhi () {        global $site _name;        echo "hello! Welcome to $site _name! ";    }    ? >

Examples of how a global variable is referenced in a function are as follows:

The results of the output are as follows:

VAR2 is set to ' xxxxxx '
VAR2 is set to ' eeee '

Visible, in the code above:
$var 2 =& $var 1; 1 is visible only inside the function. and $GLOBALS ["Var2"] =& $var 1; 2 is visible in the global scope.

We hope this article will introduce the problem of invalid PHP global is helpful!

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.