Instructions for using static in php functions_PHP tutorial

WBOY
Release: 2016-07-21 15:18:56
Original
975 people have browsed it

Copy code The code is as follows:

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

Look at the PW source code I discovered that the static keyword is used in the setHeader() function, which is very strange. I have never used it in this way before.

static is used in functions. After declaring a variable once, if the function is called again, it will continue at the initial value. For example, $sapi will be accumulated here.

Copy code The code is as follows:

echo sendHeader(1)."
";
echo sendHeader(2)."
";
echo sendHeader(3)."
";

 output:

Copy code The code is as follows:

apache2handler
apache2handles
apache2handlet

It is somewhat similar to global, but The difference is scope. static can only be used on this function.

It’s interesting. Needs further research.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325414.htmlTechArticleCopy the code The code is as follows: 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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!