Use static variables in php functions

Source: Internet
Author: User
Tags sapi
This article briefly introduces the usage of static variables in php functions. For more information, see. 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.

This article briefly introduces the usage of static variables in php functions. For more information, see.

The instance code is as follows:

  1. Function sendHeader ($ num, $ rtarr = null)
  2. {
  3. Static $ sapi = null;
  4. If ($ sapi = null)
  5. {
  6. $ Sapi = php_sapi_name ();
  7. }
  8. 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 instance code is as follows:

  1. ? Echo sendHeader (1 )."
    ";
  2. Echo sendHeader (2 )."
    ";
  3. Echo sendHeader (3 )."
    ";
  4.  
  5. Output:
  6. ? Apache2handler
  7. Apache2handles
  8. Apache2handlet

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

A little interesting. you need to study it in depth.

The instance code is as follows:

  1. Class test
  2. {
  3. Public static function (){}
  4. Public function B (){}
  5. }
  6. $ Obj = new test;

Call code

The instance code is as follows:

  1. Test: ();
  2. $ Obj-> ();
  3. $ Obj-> B ();

Another important feature of the variable range is static variable. static variables only exist in local function domains, but their values are not lost when the program runs out of this scope.

Example of static variables

The instance code is as follows:

  1. Function test ()
  2. {
  3. Static $ w3sky = 0;
  4. Echo $ w3sky;
  5. $ W3sky ++;
  6. }
  7. ?>

Now, every time you call the test () function, the $ w3sky value will be output and added.

Related Article

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.