What is a global variable? Explanation of the range of php variables

Source: Internet
Author: User
Global variables, also known as external variables, are defined outside the function. Their scope is from the definition of variables to the end of the program file. What is a global variable?

Global variablesIt is also called an external variable, which is defined externally by the function. its scope is from the definition of the variable to the end of the program file. Unlike other programming languages, global variables are not automatically set to available. In the previous section, we introduced the local variables in the range of php variables. in PHP, because functions can be considered as separate program code snippets, local variables overwrite the visibility of global variables, therefore, you cannot directly call global variables in functions.

The code format is as follows:

 ";} Sum (); // call a function?>

After the program is executed, the output result is as follows:

Calculation result: 0 // if the two variables do not have an initial value, the value is NULL. after adding the two NULL values, the result is 0.

In the above example, two global variables $ one and $ twe are declared outside the sum () function, but in PHP, global variables cannot be used directly in the function. Therefore, the variables used in the sum () function are $ one and $ twe, which are equivalent to two re-declared variables and are not assigned an initial value. They are two undefined null values, therefore, the result is 0. To use a global variable in a function, you must use a keyword to define the target variable. the keyword of this global variable is"Global", Using the keyword" global ", you can import global data to a local range of a function to tell the function subject that this variable is a global variable.

The keyword"GlobalFor example:

 ";} Sum (); // call a function?>

After the program is executed, the output result is as follows:

The calculation result is 30 // you can use the global keyword to load the global variable and use it in the function.

The "global" keyword is used to import global variables. It seems to work well and very easy, but you still need to worry about using the "global" keyword to define global data. There are three reasons:

1. code reuse is almost impossible.

If a function depends on global variables, it is almost impossible to use this function in different environments. Another problem is that you cannot extract this function and use it in other code.

2. it is very difficult to debug and solve the problem.

It is more difficult to trace a global variable than to trace a non-global variable. A global variable may be redefined in some non-obvious include files, even if you have a very good program editor (or IDE) to help you, it takes you several hours to discover the problem.

3. it is very difficult to understand the code.

It is hard to figure out where a global variable comes from and what it is used. During development, you may know every global variable, but after about a year, you may forget at least general global variables, at this time, you will regret using so many global variables.

$ GLOBALS

Use global variables in functions. in addition to the global keyword, you can also use special php custom variables.$ GLOBALSArray. The previous example can be written as replacing global with $ GLOBALS.

The code is shown as follows:

 

Note: 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 it is a super global variable.

The above is the definition of global variables and the introduction of global variables in the scope of php variables. the next chapter will introduce what is php static variables.

[Recommended tutorials]

1. php1.cn Dugu jiujian (4)-php video tutorial

2. php programming from getting started to mastering a full set of video tutorials

3. php video tutorial

What is the global variable? For more information about the scope of php variables and the details of global variable instances, see other related articles in the first PHP community!

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.