Home Backend Development PHP Tutorial Using static variables in php functions_PHP tutorial

Using static variables in php functions_PHP tutorial

Jul 13, 2016 pm 05:13 PM
php static one time introduce use about function variable exist article of Simple

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)."<br>";
echo sendHeader(2)."<br>";
echo sendHeader(3)."<br>";


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
 代码如下 复制代码
<?php
function test()
{
static $w3sky = 0;
echo $w3sky;
$w3sky++;
}
?>

?echo sendHeader(1)."<br>";

echo sendHeader(2)."<br>";
echo sendHeader(3)."<br>";

?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
<?php<🎜> 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 =...
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

How To Set Up Visual Studio Code (VS Code) for PHP Development

See all articles