Variables in PHP are divided into five types: local variables, variable functions, global variables, dynamic variables, and dynamic variables. Now I will give you a detailed introduction to the use of these five PHP variables. Friends who need to know more Available for reference.
Static variables
The storage space of dynamic variables will be automatically released after the function is executed. For static variables, when the function is executed, the storage space still stores its variables. If you want to use the variable results of the function to perform calculations, you need to set the variable as a static variable.
The way to set PHP static variables is to add the "static" symbol before the variable.
Example:
The code is as follows | Copy code | ||||
static $i=0; echo "$i "; $i++; } fun(); fun(); ?>
|
When we define a variable, it is equivalent to applying for a storage space on the server. Dynamic variables are quantities whose values are constantly changing. The characteristic is that when the function is completed, the value of its variable storage space will be automatically released. Just like a truck pulling goods, unloading it after it reaches the destination, and then returning to pull it.
Example:
代码如下 | 复制代码 |
function fun(){ |
The code is as follows | Copy code |
function fun(){ |
We learned earlier about local variables, whose values are only valid within a certain range. If you want to implement cross-domain calls, you need to use global variables.
Definition of global variables in PHP: To declare a global variable, just add "global" before the variable name. After defining the global variable, the variables of the main program can be called inside the function. Similarly, the variables within the function can also be called. Used by the main program.
代码如下 | 复制代码 |
$a=1; |
The code is as follows | Copy code |
$a=1; function fun(){ echo $a ." "; /* $a is a local variable and does not display the content */ global $a; /* Define $a as a global variable */ echo $a ." "; global $b; $b=10; } fun(); echo $b; ?> |
Local variables
In the PHP language, variables have a certain scope. Once they leave, the variables will lose their meaning or change, which is called variable scope. Just like a local area network, it is only valid in the established office or dormitory. Once it exceeds this range, it will not be able to connect to the network. Variables can be divided into local variables and global variables according to their scope. Here we will talk about local variables first.
PHP local variable value Variables defined within the function are only used within the function and are invalid outside the function; similarly, variables defined outside the function are not valid within the function if they are not passed.
代码如下 | 复制代码 |
function fun(){ $a1=5; echo $a2; } fun(); $a2=10; echo $a1; ?> |
The above code outputs nothing after running.
Variable function
Variables can not only be defined by some English letters and other characters, but also can be defined for functions. If you add the "()" symbol after a variable name, PHP will look for a function with the same value as the variable to execute, which is called a variable function.
Example:
The code is as follows
|
Copy code | ||||
http: //www.bkjia.com/PHPjc/633072.html