This article briefly introduces the use of static variables in PHP functions, there is a need to understand the students can refer to.
The code is as follows |
Copy Code |
function Sendheader ($num, $rtarr = null) { static $SAPI = null; if ($sapi = = = null) { $sapi = Php_sapi_name (); } return $SAPI + +; |
Look at the PW source code to find the SetHeader () function using the static keyword, it is strange, has not been used before.
Static is used in the function, once the variable is declared, if the function is called again, it will continue in the initial value, such as $sapi will accumulate here.
The code is as follows |
Copy Code |
? Echo Sendheader (1). " "; Echo Sendheader (2). " "; Echo Sendheader (3). " "; Output
? apache2handler Apache2handles Apache2handlet |
It's a bit similar to global, but the difference is scope. Static can only act on this function.
It's kind of interesting. Need to be studied in depth.
The code is as follows |
Copy Code |
Class Test { public static function A () {} Public Function B () {} } $obj = new test; |
Calling code
The code is as follows |
Copy Code |
Test::a (); $obj->a (); $obj->b (); |
Another important feature of the variable range is the static variable (variable). Static variables exist only in local function domains, but their values are not lost when program execution leaves this scope
Examples of using static variables
The code is as follows |
Copy Code |
function test () { static $w 3sky = 0; echo $w 3sky; $w 3sky++; } ?>
|
Now, each call to the test () function outputs the value of $w 3sky and adds one.
For more detailed information, please see: http://www.bKjia.c0m/phper/php/php-static.htm
http://www.bkjia.com/PHPjc/629126.html www.bkjia.com true http://www.bkjia.com/PHPjc/629126.html techarticle This article briefly introduces the use of static variables in PHP functions, there is a need to understand the students can refer to. The code below copies code function Sendheader ($num, $rtarr = ...