What is a global variable? Examples of global variables for the range of PHP variables

Source: Internet
Author: User
What is a global variable?

A global variable , also known as an external variable, is defined outside the function and is scoped to the end of the program file, starting at the definition of the variable. Unlike other programming languages, global variables are not automatically set to be available. In the previous section we introduced the local variables of the scope of the PHP variable , in PHP, because the function can be treated as a separate program snippet, so the local variable overrides the visibility of the global variable, so the global variable cannot be called directly in the function.

Its code is formatted as follows:

<?php $one = 10; $two = 20; The test cannot use the global variable directly inside the function $one and $two function sum () {   //a new declaration within the function and no initial value assigned to the two variable   echo "operation result:". $one + $two). " <br> "; } sum ();    Call Function?>

The output of the program after execution is shown as follows:

Operation Result: 0             //Two variables no initial value is NULL, two null values are added and result is 0

In the above example, the function sum () is declared outside the two global variables $one and $twe, but in PHP it is not possible to use global variables directly in the function. So the variables used in the sum () function are $one and $twe, equal to the two re-declared variables, and are not assigned the initial value, are two undefined null values, so the result is 0. To use a global variable in a function, you must define the target variable with the keyword " global ", and with the keyword "global" You can import the global data into the local scope of a function to tell the function body that the variable is a global variable.

Here is an example of a global variable declared outside the function using the keyword "global" called the function:

<?php $one = 10; $two = 20; Tests use keywords inside the function to load global global variables $one and $two function sum () {   //Use keywords inside the function to load global global variables, load multiple comma-separated global   $one, $two;   //Use global variables declared outside the function   echo "Operation Result:". ( $one + $two). " <br> "; } sum ();    Call Function?>

The output of the program after execution is shown as follows:

The result of the operation is         ///using the Global keyword to load a globals variable within the function

The "global" keyword is used to import global variables. It looks like it works well, and it's simple, but you're worried about defining global data using the "global" keyword. Because there are three reasons:

1, code reuse is almost impossible.

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

2, debugging and solve the problem is very difficult.

Tracking a global variable is more difficult than tracking a non-global variable. A global variable may be redefined in some of the less obvious include files, even if you have a very Good program editor (or IDE) to help you, it will take you a few hours to discover the problem.

3. It will be very difficult to understand the code.

It's hard to figure out where a global variable comes from and what it does. In the process of development, you may know that every global variable is known, but about a year later, you may forget at least the general global variables, which you will regret for using so many global variables.

$GLOBALS

Using global variables in functions, you can also use special PHP custom $GLOBALS arrays, in addition to using the Global keyword. The previous example could be written using $GLOBALS instead of global.

The code displays as follows:

<?php $one = 10; $two = 20; function sum () {   //Use $GLOBALS access the global variable inside the function,   $GLOBALS ["one"] = $GLOBALS ["one"] + $GLOBALS ["one"];} sum ();    Call function echo $two;? >

Description: In the $GLOBALS array, each variable is an element, the key name corresponds to the variable name, and the value corresponds to the contents of the variable. $GLOBALS exists in the global scope because it is a super global variable.

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.