Analysis of global and $ GLOBALS [] in php

Source: Internet
Author: User
The global variables in PHP are a little different from those in C language. In C language, global variables take effect automatically in the function unless they are overwritten by local variables, which may cause some problems, some may carelessly change a global variable. Global variables in PHP must be declared as Global when used in functions (note that the Global keyword is useful only when defined in functions)

The global variables in PHP are a little different from those in C. In C, global variables automatically take effect in functions. Unless they are overwritten by local variables, this may cause some problems, some may carelessly change a global variable. Global variables in PHP must be declared as Global when used in functions (note that the Global keyword is useful only when defined in functions)

The global variables in PHP are a little different from those in C. In C, global variables automatically take effect in functions unless they are overwritten by local variables,

This may cause some problems. Some may carelessly change a global variable. Global variables in PHP must be declared as Global when used in functions (note that the Global keyword is useful in function definition ).

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

  

Conclusion: 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,

$ Global $ a; $ a = 123; function f () {echo $ a; // error,} // check the following function f () {global $; $ a = 123;} f (); echo $ a; // correct, available

2: global Problem Analysis:
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 define a constant in config. inc. php: define (constant name, constant value)
Then, in other places that need to be used, require 'config. inc. php ',
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.
3. Examples of Global and $ GLOBALS Arrays

Example: Use global

  

The output of the above script is "3 ". Specify the global variables $ w3sky and $ w3sky2 in the function. All referenced variables of any variable point to the global variable. PHP has no limit on the maximum number of global variables that a function can declare.
The second way to access variables globally is to use a special PHP custom $ GLOBALS array. The preceding example can be written as follows:


Replace global with $ GLOBALS.

  

In the $ GLOBALS array, each variable is an element. The key name corresponds to the variable name and the value corresponds to the variable content. $ GLOBALS exists globally because $ GLOBALS is a super global variable. The following example shows how to use a super global variable:


Examples: Examples of hyperglobal variables and scopes

  
Global means that if you declare global $ db in a file, you can reference this $ db under the Declaration.


4. I thought that both global and $ GLOBALS had the same writing style, but in actual application, we found that the difference between the two is still very big!
Let's take a look at the following example:

  

The execution result is:
0
5
How can this happen? Shouldn't it be 2 or 5? How can one 0 and one 5 appear?
Well, let's keep the above questions for an in-depth analysis of the principles of $ GLOBALS and global!
We all know that variables are actually the "code" in the corresponding physical code. Suppose the memory allocated by the three variables we declared above is as follows:
$ GLOBALS reference to the php manual:
Global variable: $ GLOBALS
Note: $ GLOBALS is applicable to PHP 3.0.0 and later versions.
An array composed of all defined global variables. The variable name is the index of the array.
This is a "superglobal", or it can be described as an automatic global variable.
That is to say, $ var1 and $ GLOBALS ['var1'] in the above Code refer to the same variable, rather than two different variables!


Next, let's analyze what global has done?
We all know that all the variables produced by functions in php are private variables of the function, so the variables generated by the global keyword certainly cannot escape this rule. Why do we say this? Let's look at the following code:

  


The execution result is:
1
Why does output 1? Didn't $ a have been given to unset? Unset failed? Php bug?
None of them. In fact, the unset function works. It is used to drop $ a from the test function to the unset function. You can add it after the function.

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.