Global explanation of global variables in PHP

Source: Internet
Author: User
This article will introduce in detail the global method of the global variable in PHP. the scope of the variable is the context defined by it (that is, its effective range ). Most PHP variables have only one separate range. This independent range span also contains the include

This article introduces the global method of global variables in PHP in detail. if you need to know how to use global functions, refer to this article.

The scope of a variable is the context defined by it (that is, its effective range ). most PHP variables only have a single range. this independent range span also contains the files introduced by include and require.

The instance code is as follows:

  1. $ A = 1;
  2. Include 'B. Inc ';
  3. ?>

Here the variable $ a will include file B. takes effect in inc. however, in user-defined functions, a local function range will be introduced. by default, any variable used in the function will be restricted within the range of a local function. for example:

The instance code is as follows:

  1. $ A = 1;/* global scope */
  2. Function Test ()
  3. {
  4. Echo $ a;/* reference to local scope variable */
  5. }
  6. Test ();
  7. ?>

This script does not have any output, because the echo statement references a local version of variable $ a and is not assigned a value within this range. you may notice that the global variables in PHP are a little different from those in C language. in C language, global variables automatically take effect in functions unless they are overwritten by local variables. this may cause some problems. some may accidentally change a global variable. global variables in PHP must be declared as global when used in functions.

Today we have encountered the problem that php global variables do not work. First, let's use the following simple code:

The instance code is as follows:

  1. $ A = 0;
  2. Function Test ()
  3. {
  4. $ A = 1;
  5. }
  6. Test ();
  7. Echo $;
  8. ?>

The output in the above code is 0, because the $ a variable in the Test function is set as a local variable by default, and the $ a scope is in the Test. modify the code as follows:

The instance code is as follows:

  1. $ A = 0;
  2. Function Test ()
  3. {
  4. Global $ a; // declare that the $ a variable used in the Test function is a global variable.
  5. $ A = 1;
  6. }
  7. Test ();
  8. Echo $;
  9. ?>

After the $ a variable used in the Test function is declared as a global variable, the output of $ a is 1.

The above example is just a basic knowledge of global variables. let's look at the complex points:

// A. php file

The instance code is as follows:

  1. Function Test_Global ()
  2. {
  3. Include 'B. php ';
  4. Test ();
  5. }
  6. $ A = 0;
  7. Test_Global ();
  8. Echo $;
  9. ?>
  10. // B. php file
  11. Function Test ()
  12. {
  13. Global $ a; // declare that the $ a variable used in the Sum function is a global variable.
  14. $ A = 1;
  15. }
  16. ?>

Why is the output 0 ?!!

In user-defined functions, a local function range will be introduced. any variables used in the function will be limited to the local function scope by default (including the variables in the include and require imported files )!

Explanation:. test_Global in the PHP file is a defined third-party function, which is imported into B using include. the global variable of $ a in the PHP file, so $ a is restricted to the range of local functions of Test_Global, so B. $ a in the PHP file takes effect in Test_Global, instead of. php ....

Solution:

1. rush out of the local function

The instance code is as follows:

  1. // A. php file
  2. Function Test_Global ()
  3. {
  4. Test ();
  5. }
  6. Include 'B. php'; // Remove include from the local Test_Global function
  7. $ A = 0;
  8. Test_Global ();
  9. Echo $;
  10. ?>
  11. // B. php file
  12. Function Test ()
  13. {
  14. Global $;
  15. $ A = 1;
  16. }
  17. ?>

2. excellent accessors

The instance code is as follows:

  1. // A. php file
  2. Include 'B. php ';
  3. $ A = 0;
  4. Set_Global ($ );
  5. Echo $;
  6. ?>
  7. // B. php file
  8. Function Set_Global (& $ var)
  9. {
  10. $ Var = 1;
  11. }
  12. ?>

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.