Invalid PHP global variable

Source: Internet
Author: User

For beginners in PHP, global when using a keyword, you might find that global a variable outside of a function in a function cannot output the variable correctly in some cases (that is, the global variable is invalid). Let's look at a simple and common example.

Here we have two pages of a.php and b.php.

The b.php page code is as follows:

    1. <? PHP
    2. $site _name = ' Codeplayer ' ;
    3. function Sayhi (){
    4. Global $site _name;
    5. Echo "hello! Welcome to $site _name! " ;
    6. }
    7. ?>

The a.php page code is as follows:

    1. <? PHP
    2. function Include_view_page (){
    3. include ' b.php ' ;
    4. Sayhi ();                    
    5. }
    6. Include_view_page ();
    7. ?>

The example above is very simple and we want to be able to display the welcome statement correctly when we visit the a.php page. However, unfortunately, when we use the browser to access the a.php page, we find that the output is as follows:

Hello! Welcome to!

In other words, when we call a function in a function include_view_page() sayHi() , the b.php page sayHi() function is global $site_name not correctly recognized and effective. What the hell is going on here?

In fact, when we b.php a page in a function, the variable on the include_view_page() include b.php page is equivalent to the scope $site_name include_view_page() within the function. It is well known that within global a function a variable actually establishes a reference to a page global variable within a function. In our case, this $site_name variable is only a local variable within the function for a.php, so it is include_view_page() not possible for the variable to get the global correct variable and variable value when we make the related call.

In PHP, we need to pay particular attention to the problem of include changing the scope of variables in the page, which is similar to the one in the function above. To avoid this, we should try to minimize the number of include calls and try not to use them within the function include . In addition, we can declare $site_name in the form of a global variable on the b.php page.

    1. //b.php
    2. < ? php
    3. global $site _name
    4. $site _name = ' Codeplayer '
    5. function Sayhi () {
    6.     global $site _name ;
    7.     echo "hello! Welcome to $site _name! "
    8. }
    9. ?>

turn from: http://www.365mini.com/page/php-global-invalid.htm

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.