Instructions for using static in the php function

  • 2020-05-17 05:00:13
  • OfStack

 
function sendHeader($num, $rtarr = null) { 
static $sapi = null; 
if ($sapi === null) { 
$sapi = php_sapi_name(); 
} 
return $sapi++; 

Looking at the PW source code, I noticed that the setHeader() function USES the static keyword, which is strange and has never been used before.

static is used in a function. After declaring the variable once, if the function is called again, it will continue at the initial value, such as $sapi, which will accumulate.

 
echo sendHeader(1)."<br>"; 
echo sendHeader(2)."<br>"; 
echo sendHeader(3)."<br>"; 

output:

 
apache2handler 
apache2handles 
apache2handlet 

It's similar to global, but different in scope. static can only operate on this function.

It's kind of interesting. It needs further study.


Related articles: