Home >Backend Development >PHP Tutorial >Tutorial on using static_PHP in php functions
function sendHeader($num, $rtarr = null) {
static $sapi = null;
If ($sapi === null) {
$sapi = php_sapi_name();
}
Return $sapi++;
When looking at the PW source code, I found that the static keyword is used in the setHeader() function, which is very strange. It has never been used in this way before.
Static is used in functions. After a variable is declared once, if the function is called again, the initial value will be continued. For example, $sapi will be accumulated.
echo sendHeader(1)."
";
echo sendHeader(2)."
";
echo sendHeader(3)."
";
Output:
apache2handler
apache2handles
apache2handlet
It is similar to global, but the difference is the scope. static can only be used on this function.
Interesting. Needs further research.
Excerpted from zaric