_php tutorial using static variables in PHP functions

Source: Internet
Author: User
Tags sapi
This article briefly introduces the use of static variables in PHP functions, there is a need to understand the students can refer to.
The code is as follows Copy Code

function Sendheader ($num, $rtarr = null)
{
static $SAPI = null;
if ($sapi = = = null)
{
$sapi = Php_sapi_name ();
}
return $SAPI + +;


Look at the PW source code to find the SetHeader () function using the static keyword, it is strange, has not been used before.

Static is used in the function, once the variable is declared, if the function is called again, it will continue in the initial value, such as $sapi will accumulate here.

The code is as follows Copy Code

? Echo Sendheader (1). "
";
Echo Sendheader (2). "
";
Echo Sendheader (3). "
";


Output

? apache2handler
Apache2handles
Apache2handlet

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

It's kind of interesting. Need to be studied in depth.

The code is as follows Copy Code
Class Test
{
public static function A () {}
Public Function B () {}
}
$obj = new test;

Calling code

The code is as follows Copy Code

Test::a ();
$obj->a ();
$obj->b ();

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

Examples of using static variables

The code is as follows Copy Code
function test ()
{
static $w 3sky = 0;
echo $w 3sky;
$w 3sky++;
}
?>

Now, each call to the test () function outputs the value of $w 3sky and adds one.


For more detailed information, please see: http://www.bKjia.c0m/phper/php/php-static.htm

http://www.bkjia.com/PHPjc/629126.html www.bkjia.com true http://www.bkjia.com/PHPjc/629126.html techarticle This article briefly introduces the use of static variables in PHP functions, there is a need to understand the students can refer to. The code below copies code function Sendheader ($num, $rtarr = ...

  • 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.