When using a variable, it must comply with the definition rules of the variable. Variables must be used within the valid range. if the variable is out of the limited range, the variable will be meaningless. just like we have one hundred yuan, we can buy less than one hundred yuan, if the cost of an item exceeds one hundred yuan, the item cannot be used. One hundred RMB is equivalent to this variable, and the range within one hundred RMB is equivalent to the scope.
What is the variable scope?
When using a variable, it must comply with the definition rules of the variable. Variables must be used within the valid range. if the variable is out of the limited range, the variable will be meaningless. just like we have one hundred yuan, we can buy less than one hundred yuan, if the cost of an item exceeds one hundred yuan, the item cannot be used. One hundred RMB is equivalent to this variable, and the range within one hundred RMB is equivalent to the scope.
Due to different scopes, PHP variables include local variables, global variables, and static variables, as shown in the following table:
Scope |
Description |
Local variable |
For variables defined within a function, the scope is the function range. |
Global variables |
The scope of variables other than all defined functions is the entire php file, but they cannot be used within the user-defined functions. If you want to use global variables within a user-defined function, useGlobalKeyword declaration global variable |
Static variables |
The variable value can be retained after the function call ends. when it returns to its scope again, it can continue to use the original value. Generally, after a function is called, the stored data value is cleared and the occupied memory space is released. When using static variables, use keywords firstStaticTo declare the variable, put the keyword static before the variable to be defined |
A variable defined within a function has the function scope. if a value is assigned outside the function, it is considered to be another variable completely different. When you exit a function that declares a variable, the variable and the corresponding value are cleared.
Instance
This example is used to compare the variables assigned in the function (local variables) and the variables assigned outside the function (global variables). The instance code is as follows:
"; // Output local variable} add (); // call function echo" the output content outside the function is: ". $ exam; // output global variable?>
The running result is as follows:
Static variables can be used in many places. For example, you can use static variables in a blog to record the number of viewers. each time a user visits or leaves, the current number of viewers can be retained. Static variables can also be used in chat rooms to record users' chat content.
Example
Use static variables and common variables in the following example to output a data, and check whether the functions of the two are different. the code is as follows:
"; For ($ I = 0; $ I <10; $ I ++) zdy1 (); // outputs 10 1 echo"
";?>
Code running result:
Instance details:
The custom function zdy () is output from 1 ~ 10 contains 10 numbers in total, while the zdy1 () function outputs 10 numbers ., because the custom function zdy () contains the static variable $ message, $ message in the function zdy1 () is a common variable. Both variables are initialized to 0, and two functions are called using the for loop respectively. The result function zdy () retains the value of the static variable $ message after being called. Static Variable initialization is performed only when the first call of the function is executed, and will not be initialized later. After the function zdy1 () is called, its variable $ message loses its original value and is reinitialized to 0.
Global variables can be accessed anywhere in the program, but they cannot be used within a user-defined function. If you want to use global variables within a user-defined function, you must use the global keyword to declare global variables.
Example
The following is a comparison between applying global variables in user-defined functions and not applying global variables. In this example, two global variables $ zy and $ zyy are defined. in the user-defined function lxt (), you want to call them in line 5 and 7, and the program outputs only $ zyy.
Because the global keyword $ zyy is declared in row 6th. The first line does not have any output, and $ zy has nothing to do with $ zy in the second line. The instance code is as follows:
"; // $ Zy cannot be called, and global $ zyy is not output; // The keyword global is used in the function. it is not considered as a private variable echo $ zyy ."
"; // Call $ zyy} lxt ();?>
Code running result:
This is the difference between applying global variables and not applying global variables. in the next section, we will explain "Variable variables"
Related video tutorials are recommended: Scope of variables in php1.cn Dugu Jiuyu (4)-php video tutorial
The above is a detailed description of the PHP variable scope instance. For more information, see other related articles in the first PHP community!