PHP global vs. ¥globals[] Differences

Source: Internet
Author: User
Difference between global and ¥globals[in PHP

To develop a PHP program, it is unavoidable to use the global data.

Global Variables are one of them, of course, this view is controversial, many people still recommend disabling global variables, improper use of the resulting program readability is poor! The structure is chaotic, the debugging is confused, but cannot deny his convenience, this is why PHP uses Global variables Globally why is it!...

Today, we encountered a problem where the PHP global variables did not work.

Let's start with a simple code:

1

2 $a= 0 ;

3 functionTest()

4 {

5 $a=1;

6 }

7 Test();

8 echo$a;

9 ?>

The output in the above code is 0, because the $ A variable in the function body test is defaulted to a local variable, and the scope of the $a is within test. Modify the code as follows

01

02 $a= 0 ;

03 functionTest()

04 {

05 global$a;//申明函数体Test内使用的$a变量为global全局变量

06 $a=1;

07 }

08 Test();

09 echo$a;

10 ?>

Declare that the $ A variable used within the function body test is the global global variable, so that $ A has the overall effect, so the output is 1.
The above example is just the basic global variables knowledge, below we look at the complex point:

01 //A.php 文件

02

03

04 functionTest_Global()

05 {

06 include'B.php';

07 Test();

08 }

09

10 $a= 0 ;

11 Test_Global();

12 echo$a;

13 ?>

14

15 //B.php 文件

16

17

18 functionTest()

19 {

20 global$a;//申明函数体Sum内使用的$a变量为global全局变量

21 $a=1;

22 }

23 ?>

Why is the output 0?!!

In a user-defined function, a local function scope is introduced. any variables that are used inside the function will be limited to the local function (including variables within the import file of include and require) by default!
Explanation: The internal test_global of the a.php file is a well-defined third-party function that imports $ A in the global global variable of $ A in the b.php file with include, so the $a is limited to the scope of the Test_global local function , So the $ A within the b.php file is within Test_global, not the whole a.php ....

Solution:

1. Punch out the local function

01 //A.php 文件

02

03

04 functionTest_Global()

05 {

06 Test();

07 }

08 include'B.php'; //将include 从局部Test_Global函数中移出

09 $a= 0 ;

10 Test_Global();

11 echo$a;

12 ?>

13

14 //B.php 文件

15

16

17 functionTest()

18 {

19 global$a;

20 $a=1;

21 }

22 ?>

2. Excellent access to the viewer

1 //A.php 文件

01

02 include'B.php';

03 $a=0;

04 Set_Global($a);

05 echo$a;

06 ?>

07

08 //B.php 文件

09

10

11 functionSet_Global(&$var)

12 {

13 $var=1;

14 }

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