Use of Global in PHP

Source: Internet
Author: User
Variables are divided into global variables and local variables. Children who have learned the C language know that the scope of the global variable is the entire file. It works even inside the function, but in PHP, if you use a global variable in a function, PHP will assume that the variable is undefined. If we need to use this global variable inside the function, then we need to add the keyword Global to the inside of the function, before the global variable. Here is a small demo of your own. To prove what I said.


    
     $str = "string";    functiontest()    {if (isset($str))        {          echo"the string is defined";       }       else       {          echo"the string is undefined";       }    }    test();?>

This is the result of running in the browser:
The string is undefined


    
     $str = "string";    functiontest()    {global$str;//上面的test函数中没有这句话if (isset($str))        {          echo"the string is defined";       }       else       {          echo"the string is undefined";       }    }    test();?>

This is the result of running in the browser:
The string is defined

$GLOBALS-Referencing all variables available in the global scope

A globally combined array that contains all the variables. The name of the variable is the key of the array.


    
     functiontest() {$foo = "local variable";    echo'$foo in global scope: ' . $GLOBALS["foo"] . "\n";    echo'$foo in current scope: ' . $foo . "\n";}$foo = "Example content";test();?>

The output of the above routines is similar to the following:

$fooinglobal scope: Example content$fooinlocalvariable

'). addclass (' pre-numbering '). Hide (); $ (this). addclass (' has-numbering '). Parent (). append ($numbering); for (i = 1; i <= lines; i++) {$numbering. Append ($ ('
  • '). Text (i)); }; $numbering. FadeIn (1700); }); });

    The above describes the use of global in PHP, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    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.