Use static in php functions _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Tags sapi
Use static in php functions. Copy the code as follows: functionsendHeader ($ num, $ rtarrnull) {static $ sapinull; if ($ sapinull) {$ sapiphp_sapi_name ();} return $ sapi ++; when reading PW source code, we found The code is as follows:


Function sendHeader ($ num, $ rtarr = null ){
Static $ sapi = null;
If ($ sapi === null ){
$ Sapi = php_sapi_name ();
}
Return $ sapi ++;


When reading the PW source code, I found that the setHeader () function uses the static keyword. it is strange that it has never been used before.

Static is used in the function. after a variable is declared, if this function is called again, it will continue at the initial value. for example, $ sapi will accumulate here.

The code is as follows:


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


Output:

The code is as follows:


Apache2handler
Apache2handles
Apache2handlet


It is a bit similar to global, but the difference is the scope. Static can only act on this function.

A little interesting. In-depth research is required.

The http://www.bkjia.com/PHPjc/325414.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/325414.htmlTechArticle code is as follows: function sendHeader ($ num, $ rtarr = null) {static $ sapi = null; if ($ sapi = null) {$ sapi = php_sapi_name ();} return $ sapi ++; found when reading PW source code...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.