Like most structured programs, there are so-called global variables and local variables. php also has the same processing method in this respect. When php programs are executed, the system retains a region of global variables in the memory. You can use
Like most structured programs, there are so-called global variables and local variables. php also has the same processing method in this respect.
When php programs are executed, the system retains a region of global variables in the memory. You can use $ globals ['variable name'] to retrieve the required variables. In user-defined functions or programs, you can use the $ globals array to retrieve the required variables. Of course, don't forget that php variables are case-sensitive. if the case is incorrect, it may be one hundred years, and the variables won't come out.
$ Globals array is a variable that is more specific to a php program and does not need to be defined. The system will actively match the relevant variable in it. In functions, you do not need to worry about whether the $ globals array has been globally defined.
Similar to the $ globals variable, there are $ php_errormsg string variables. If the track_errors option in the php configuration file (php. ini/php3.ini) is enabled, the global variable $ php_errormsg can be used to view the error message.
In php, the scope of global variables is limited to important programs and does not affect variables with the same name in the function, that is, global variables and local variables do not infringe upon each other. To make variables accessible to functions, use the $ globals array or apply the global definition.
For example, in a self-developed function, to obtain the file name of the current php program page, you can use $ globals ['php _ self '] to retrieve the value of $ php_self.
// The Application $ globals array
Function myfunc (){
Echo $ globals ['php _ self '];
}
Myfunc ();
?>
The following is an incorrect demonstration. do not imitate it. the above is an accurate demonstration.
// This is an incorrect example
Function errfunc (){
Echo $ php_self;
}
Errfunc ();
?>
However, if the example for this error is changed to the following example, the title will be removed.
// Global definition of the application
Function myfunc (){
Global $ php_self;
Echo $ php_self;
}
Myfunc ();
?>
Add the global name before the variable to define the variable as a global variable. Using this method, you do not need to apply the $ globals array, but also let variables into self-developed functions.
Next, let's look at the example of static variables.
// Example of static variables
Function myfunc (){
Static $ mystr;
$ Mystr. = 'ha ';
Echo $ mystr .'
\ N ';
}
Myfunc (); // ha
Myfunc (); // haha
Myfunc (); // hahaha
?>
The variables generated when the function is executed will dissipate at the end of the function. sometimes, due to the needs of the program, the function is in a loop, if you don't expect that the variable will dissipate after each function is executed, static variable will be useful. In the above example, before applying the $ mystr variable, add static before the variable, that is, the variable $ mystr is a static variable. when every time the myfunc () function is executed, the value of $ mystr will increase continuously, and each time the result is executed, there will be one more character. If the definition of static variables is removed, there is no way to accumulate the characters.
// Example of a variable that is not static (incorrect)
Function myfunc (){
$ Mystr. = 'ha ';