PHP global variable defines the current page of the Globals variable _php tutorial

Source: Internet
Author: User

The role of the 1:php global variable is to define global variables, but this global variable is not applied to the entire site, but to the current page, including all files of include or require

    1. $ a = 123 ;
    2. function AA ()
    3. {
    4. Global $a;
    5. If you do not define $ A as the global variable
      , the function body is not accessible to $ A
    6. echo $a;
    7. }
    8. AA ();

Summary: The PHP global variable defined in the function body can be used outside the body, and the global variable defined outside the function body cannot be used within the function body.

 
  
  
  1. $glpbal $a;
  2. $ a = 123 ;
  3. function f ()
  4. {
  5. echo $a; Error
  6. }

Take a look at the following example

 
  
  
  1. function f ()
  2. {
  3. Global $a;
  4. $ a = 123 ;
  5. }
  6. f ();

2:php global variable Problem Resolution:

Question: I defined some variables ($a) in the config.inc.php, and in other files the function external include ("config.inc.php"), the function internal need to use these variable $ A, if there is no declaration, echo $ A is not printing anything. So declaring the global $a, but there are a lot of functions and a lot of variables, you can't always repeat this statement? If there is any good solution, please advise.

Answer1: Define constants in config.inc.php first: define (constant name, constant value), and then require ' config.inc.php ' in other places where it is needed, and then you can use the constant in this file directly.

Answer2: I also have a way of defining arrays, such as $x[a], $x, so just declare the global $x one.

Answer3: I tried this method of yours, No.

Answer4: Change your php.ini file.

Set the PHP global variable to ON


http://www.bkjia.com/PHPjc/446106.html www.bkjia.com true http://www.bkjia.com/PHPjc/446106.html techarticle the role of the 1:php global variable is to define global variables, but this global variable is not applied to the entire site, but to the current page, including all files of include or require $ A = 123 ...

  • 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.