Use of the Global keyword in PHP

Source: Internet
Author: User
The amount is 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.

<?php     $str = "string";    function 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:

<?php     $str = "string";    function test ()    {         global $str;//The above test function does not have this sentence       if (isset ($STR))        {          echo "the string is Defined ";       }       else        {          echo "the string is undefined";       }    }    Test ();? >

This is the result of running in the browser:

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.