Static variables only exist in the function scope. That is to say, static variables only exist in the stack. Generally, variables in a function are released after the function ends, such as local variables, but static variables do not. That is to say, when you call this function again, the value of this variable will be retained.
Simplest definition method
The define () function defines a constant.
Constants are similar to variables, except that:
After setting, the constant value cannot be changed.
The constant name does not need to start with the dollar sign ($)
The scope does not affect constant access.
A constant value can only be a string or number.
Syntax
Define (name, value, case_insensitive)
<? Php Tutorial
Define ("year", "2012 ");
Define ("month", "12 ");
Define ("date", "21 ");
Define ("thing", "doomsday ");
Echo year. "-". month. "-". date. "". thing;
?>
Static is used for variables. The storage unit of the variable is declared for static allocation. The storage unit of the variable does not change from the beginning to the end of the program operation. Static is often used in global quantity. First, the values in it are always valid and do not disappear because of the input and exit subprograms. In addition, static variable operations are faster than non-static allocation.
<? Php
Class foo {
Static $ my_static = 5;
Public $ my_prop = 'bla ';
}
Print foo: $ my_static; copyright dedecms
$ Obj = new foo;
Print $ obj-> my_prop;
?>
Const is a constant, that is, the constant value from the beginning to the end of the program operation.
<? Php
Class say_const {
Const charset = "China ";
Publice function say_hello (){
Echo slef: charset;
}
}
$ Const1 = new say_const ()'
$ Const1-> say_hello ();
?>
The output is "China"
The following is a complete static variable.
Function write_file ($ file, $ msg, $ usecheck = 5 ){
Static $ check = 1; // used here ~
$ F = @ fopen ($ file, "a + B ");
If ($ f ){
If (flock ($ f, lock_ex )){
Fwrite ($ f, $ msg );
Flock ($ f, lock_un );
Fclose ($ f );
} Else {
Fclose ($ f );
$ Check ++;
If ($ check <= $ usecheck ){
Sleep (1); // pause the program and wait for other processes to release the resource.
Write_file ($ file, $ msg );
}
}
} Else {
$ Check ++;
If ($ check <= $ usecheck ){
Sleep (1); // pause the program and wait for other processes to release the resource.
Write_file ($ file, $ msg );
}
}
}