In php, variables are divided into five types: local variables, variable functions, global variables, dynamic variables, and dynamic variables. Next I will introduce these five types of php variables in detail, for more information, see.
Static variables
After a function is executed, its storage space is automatically released. Static variables are stored in the bucket after the function is executed. If you want to use the function's variable results for calculation, you need to set the variable to a static variable.
The php static variable setting method is to add the "static" symbol before the variable.
Instance:
| The Code is as follows: |
Copy code |
|
<? Php
Function fun (){
Static $ I = 0;
Echo "$ I <br> ";
$ I ++;
}
Fun ();
Fun ();
?> |
Dynamic Variables
When we define a variable, it is equivalent to applying for a bucket on the server. Dynamic variables are the changing amount of values. They are characteristic that the value of the variable storage space is automatically released after the function is run. For example, a freight car pulls the goods, unload the goods after it is shipped to the destination, and then return to the pulling.
Instance:
| The Code is as follows: |
Copy code |
|
<? Php
Function fun (){
$ I = 0;
Echo "$ I <br> ";
$ I ++;
}
Fun ();
Fun ();
?> |
Global Variables
We have learned about local variables. Their values are valid only within a certain range. If you want to implement cross-origin calls, you need to use global variables.
Php defines global variables: you only need to add "global" to the declaration of global variables. After the global variables are defined, the variables of the main program can be called internally by the function, the variables in the same function can also be used by the main program.
Instance 1:
| The Code is as follows: |
Copy code |
|
<? Php
$ A = 1;
Function fun (){
Echo $ a. "<br>";/* $ a is a local variable and NO content is displayed */
Global $ a;/* defines $ a as a global variable */
Echo $ a. "<br> ";
Global $ B;
$ B = 10;
}
Fun ();
Echo $ B;
?> |
Local variable
In php, variables have a certain scope of function. Once left, variables will lose meaning or change, which is called the scope of variables. It is like a LAN, which is only valid in the established office or dormitory. Once the LAN is out of this range, the network will not be connected. The variables can be divided into local variables and global variables. Here we will talk about local variables first.
The variables defined in the php local variable value function are only used in the function and are invalid outside the function. Similarly, if the variables defined outside the function are not passed, they are also invalid in the function.
Instance:
| The Code is as follows: |
Copy code |
<? Php
Function fun (){
$ A1 = 5;
Echo $ a2;
}
Fun ();
$ A2 = 10;
Echo $ a1;
?> |
Nothing is output after the above Code is run.
Variable Functions
Variables can be defined not only by English letters and other characters, but also by functions. If you add the "()" symbol after a variable name, php searches for function execution with the same value as the variable, which is called a variable function.
Instance:
| The Code is as follows: |
Copy code |
|
<? Php
Function p ($ name ){
Echo "my name is". $ name;
}
$ Someone = "p";/* assign the string "p" to $ someone */
$ Someone ('Tom ');/* It is equivalent to $ p ('Tom') and is a variable function */
?> |