PHP Global variable defines the Global variables of the current page

Source: Internet
Author: User

1: PHP Global variable defines Global variables. However, this Global variable is not applied to the entire website, but to the current page, including all files of include or require.

  1. $ A = 123;
  2.  
  3. Function aa ()
  4. {
  5. Global $;
  6. // If $ a is not defined as a global variable
    The function body cannot access $.
  7. Echo $;
  8. }
  9. Aa ();

Conclusion: PHP Global variables defined in the function body can be used in vitro. global variables defined in the function body cannot be used in the function body,

 
  1. $ Glpbal $;
  2. $ A = 123;
  3.  
  4. Function f ()
  5. {
  6. Echo $ a; // error,
  7. }

Let's take a look at the following example:

 
  1. Function f ()
  2. {
  3. Global $;
  4. $ A = 123;
  5. }
  6.  
  7. F ();
  8. Echo $ a; // correct, which can be used

2: Analysis of PHP Global variables:

Question: In config. inc. php defines some variables ($ a). In other files, the function external include ("config. inc. php "), the function needs to use these variables $ a internally. If no declaration is made, echo $ a cannot print anything. Therefore, we declare global $ a. But there are a lot of functions and many variables that cannot be repeatedly declared? Please advise if you have any good solutions.

Answer1: first in config. inc. define a constant in php: define (constant name, constant value), and then require 'config. inc. php, and then you can directly use this constant in this file.

Answer2: I also have a way to define an array, such as $ x [a], $ x, so that we only need to declare a global $ x.

Answer3: I tried your method. No.

Answer4: Modify your php. ini file.

Set PHP Global variable to on


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.