Summary of usage of PHP variable scopes

Source: Internet
Author: User
    1. function function_name () {
    2. Global $var;
    3. }
    4. $var = 20;
    5. Function_name (); Function call.
    6. ?>
Copy Code

In this example, the $var inside the function is now the same as the $var outside the function. This means that the variable $var already has a value of 20, and if the value is changed inside the function, the external $var value will change. Another way to avoid variable scopes is to take advantage of hyper-global variables: $_get, $_post, $_request, and so on. These variables are automatically accessible within your function (so they are super global variables). You can also add elements to the $GLOBALS array so that they can be used within the function.

It is best not to use global variables within a function. When designing functions, you should make them accept each value as an argument as needed, and return any value as needed. Relying on global variables within a function makes them more dependent on the context and therefore less useful.

The main variables in PHP are: Built-in super global variables, general variables, constants, global variables, static variables, and so on.

Built-in super global variables can be used and visible anywhere in the script. That is, if we change one of the values in a PHP page, the value will change when used in other PHP pages.

constants, once declared, can be globally visible, that is, they can be used inside and outside the function, but this is limited to a single page (containing the PHP script that we included through include and include_once), but it is not available on other pages.

The global variables declared in a script are visible throughout the script, but not inside the function, if the variables inside the function are the same as the global variable names, the variables inside the function prevail.

When a variable used inside a function is declared as a global variable, its name is the same as the name of the global variable, in which case we can use global variables outside of the function in the function so that the last one can be avoided because the variable inside the function is the same as the external global variable name, which overrides the external variable.

A variable that is created inside a function and declared as static cannot be visible outside the function, but it can be persisted during the execution of a function many times, most commonly in the recursive execution of a function.

A variable created inside a function is local to the function, and when the function terminates, the variable does not exist.

The complete list of super global variables is as follows:

    1. $a = 4;
    2. function Sendvalue ($x)
    3. {
    4. echo $x;
    5. }
    6. Sendvalue ($a);
    7. ?>
Copy Code

Explanation: $a defined in the function, the function defines the parameter, and when the function is called, the $a is passed in the form of a parameter. So the above code will work.

    1. $a = 4;
    2. function Sendvalue ()
    3. {
    4. echo $a;
    5. }
    6. Sendvalue ();
    7. ?>
Copy Code

Explanation: When a function is called, $a cannot be passed as a parameter. So the above code does not work properly. The scope of a variable range variable is the context in which it is defined (translator: plainly, that is, its effective scope). Most PHP variables have a single range. This separate scope span also contains the files introduced by include and require. Example:

    1. $a = 1;
    2. Include "B.inc";
    3. ?>
Copy Code

Here the variable $a will take effect in the include file B.inc. However, 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 in the default context. Example:

    1. $a = 1; /* Global scope */

    2. function Test ()

    3. {
    4. echo $a; /* Reference to local scope variable */
    5. }

    6. Test ();

    7. ?>
Copy Code

This script does not have any output, because the Echo statement refers to a local version of the variable $a, and within that range, it is not assigned a value. You may notice that the global variables in PHP are a little different from the C language, and in C, global variables are automatically applied in functions unless overridden by local variables. This can cause some problems, and some people may inadvertently change a global variable. A global variable in PHP must be declared global when used in a function.

The Global keyword first, an example of using global:

Example 12-1. Using Global

    1. $a = 1;

    2. $b = 2;

    3. function Sum ()

    4. {
    5. Global $a, $b;

    6. $b = $a + $b;

    7. }

    8. Sum ();

    9. Echo $b;
    10. ?>
Copy Code

The output of the above script will be "3". The global variable $a and $b are declared in the function, and all reference variables of any variable are pointed to the global variable. PHP has no restrictions on the maximum number of global variables a function can declare.

The second way to access variables globally is to customize $GLOBALS arrays with special PHP. The previous example can be written as:

Example 12-2. Replace Global with $GLOBALS

    1. $a = 1;

    2. $b = 2;

    3. function Sum ()

    4. {
    5. $GLOBALS ["b"] = $GLOBALS ["a"] + $GLOBALS ["B"];
    6. }

    7. Sum ();

    8. Echo $b;
    9. ?>
Copy Code

In $GLOBALS array, each variable is an element, the key name corresponds to the variable name, and the contents of the value variable. $GLOBALS exists globally because $GLOBALS is a hyper-global variable. The following example shows the usefulness of a hyper-global variable:

Example 12-3. Examples of hyper-global variables and scopes are shown

    1. function Test_global ()

    2. {
    3. Most of the predefined variables are not "super", they need to use the ' global ' keyword to make them valid in the local area of the function.
    4. Global $HTTP _post_vars;

    5. Print $HTTP _post_vars[' name '];

    6. Superglobals are valid in any scope, and they do not require a ' global ' statement. Superglobals was introduced in PHP 4.1.0.

    7. Print $_post[' name '];
    8. }
    9. ?>
Copy Code

Another important feature of using static variable range is the static variable (variable). A static variable exists only in the local function domain, but its value is not lost when the program executes away from the scope. Take a look at the following example:

Example 12-4. Example of a static variable that needs to be shown

    1. function Test ()
    2. {
    3. $a = 0;
    4. echo $a;
    5. $a + +;
    6. }
    7. ?>
Copy Code

This function is of little use because the value of $a is set to 0 and output "0" each time it is called. Adding a variable to the $a + + does not work because the variable $a does not exist once you exit the function. To write a count function that does not lose this count value, define the variable $a as static:

Example 12-5. Examples of using static variables

    1. function Test ()
    2. {
    3. static $a = 0;
    4. echo $a;
    5. $a + +;
    6. }
    7. ?>
Copy Code

Now, each call to the Test () function outputs the value of the $a and adds one. 1 2 Next last page

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