Home > Backend Development > PHP Tutorial > Use static in php custom functions

Use static in php custom functions

WBOY
Release: 2016-07-25 09:05:18
Original
973 people have browsed it
  1. function sendHeader($num, $rtarr = null) {
  2. static $sapi = null;
  3. if ($sapi === null) {
  4. $sapi = php_sapi_name();
  5. }
  6. return $sapi++;
  7. ?>
Copy the code

When I looked at the PW source code, I found that the static keyword was used in the setHeader() function. It was strange. It had never been used like this 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.

  1. echo sendHeader(1)."
    ";
  2. echo sendHeader(2)."
    ";
  3. echo sendHeader(3)."
    ";
  4. ?>
Copy the code

Output result: apache2handler apache2handles apache2handlet

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



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