Difference between global and ¥ GLOBALS [] in PHP

Source: Internet
Author: User
In PHP, global is different from ¥ GLOBALS [] to develop a PHP program. it is inevitable to use global data. global variables are one of them. Of course this is quite controversial. many people suggest disabling global variables. Improper use may cause poor program readability! The structure is messy and debugging is even more confusing, but it is undeniable that it is convenient. This is why php uses the global variable !... What is the difference between global and ¥ GLOBALS [] in PHP today?

Develop a PHP program and use global data inevitably.

Global variablesThis is one of them. of course, this idea is quite controversial. many people still suggest disabling global variables. Improper use causes poor program readability! The structure is messy and debugging is even more difficult, but it is undeniable that it is convenient. This is why php usesGlobalWhy !...

Today we have encountered the problem that php global variables do not work.

First, a simple code:

1
2 $a = 0 ;
3 function Test()
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 Test function is set as a local variable by default, and the $ a scope is in the Test. modify the code as follows:

01
02 $a = 0 ;
03 function Test()
04 {
05  global$a;// Declare that the $ a variable used in the Test function is a global variable.
06  $a=1;
07 }
08 Test();
09 echo $a;
10 ?>

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:

01 // A. php file
02  
03
04 function Test_Global()
05 {  
06 include 'B.php';  
07 Test();  
08 }  
09  
10 $a = 0 ;
11 Test_Global();
12 echo $a;
13 ?> 
14  
15 // B. php file
16  
17
18 function Test()
19 {
20  global$a;// Declare that the $ a variable used in the Sum function is a global variable.
21  $a=1;
22 }
23 ?>

Why is the output 0 ?!!

In a user-defined function, a local function range is 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. this function imports the global variables of $ A in the B. php file using include.$ A is restricted to the range of Test_Global local functions.Therefore, $ a in the B. php file takes effect in Test_Global instead of in A. php ....

Solution: 1. rush out of the local function
01  // A. php file
02  
03
04 function Test_Global()
05 {  
06 Test();  
07 }  
08 include 'B.php';  // Remove include from the local Test_Global function
09 $a = 0 ;
10 Test_Global();
11 echo $a;
12 ?> 
13  
14 // B. php file
15  
16
17 function Test()
18 {
19  global$a;
20  $a=1;
21 }
22  ?>
2. excellent accessors
1  // A. php file
01
02 include 'B.php'; 
03 $a =0;
04 Set_Global($a);
05 echo $a;
06 ?> 
07  
08 // B. php file
09  
10
11 function Set_Global(&$var)
12 {
13    $var=1;
14 }
15  ?>

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.