Using static variables in php functions_PHP tutorial

WBOY
Release: 2016-07-13 17:13:34
Original
1017 people have browsed it

This article will briefly introduce the use of static variables in PHP functions. Students who need to know more can refer to it.

The code is as follows
 代码如下 复制代码

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

Copy code


function sendHeader($num, $rtarr = null)

{ 

static $sapi = null;
 代码如下 复制代码

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


output:

?apache2handler
apache2handles
apache2handlet

if ($sapi === null)

{    

$sapi = php_sapi_name();

return $sapi++;

 代码如下 复制代码
class test
{
public static function a(){}
public function b(){}
}
$obj = new test;

 代码如下 复制代码

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

When looking at the PW source code, I found that the static keyword is used in the setHeader() function, which is very strange. It has never been used in this way before.

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

output:
The code is as follows Copy code
 代码如下 复制代码
function test()
{
static $w3sky = 0;
echo $w3sky;
$w3sky++;
}
?>

?echo sendHeader(1)."
";

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

?apache2handler

apache2handles

apache2handlet It is similar to global, but the difference is the scope. static can only be used on this function. Interesting. Needs further research.
The code is as follows Copy code
class test { public static function a(){}
public function b(){}
} $obj = new test; Call code
The code is as follows Copy code
test::a(); $obj->a(); $obj->b(); Another important feature of variable scope is static variables. Static variables only exist in the local function scope, but their values ​​are not lost when the program execution leaves this scope Example Example of using static variables
The code is as follows Copy code
function test()<🎜> {<🎜> static $w3sky = 0;<🎜> echo $w3sky;<🎜> $w3sky++;<🎜> }<🎜> ?> Now, each call to the test() function will output the value of $w3sky and increment it by one. For more details, please see: http://www.bKjia.c0m/phper/php/php-static.htm http://www.bkjia.com/PHPjc/629126.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629126.htmlTechArticleThis article briefly introduces the usage of static variables in php functions. Students who need to know more can refer to it. one time. The code is as follows Copy code function sendHeader($num, $rtarr =...
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!