PHP static local static variables and global static variables summary_PHP tutorial

WBOY
Release: 2016-07-13 10:36:56
Original
825 people have browsed it

Characteristics of static local variables:

1. It will not change as the function is called and exits. However, although the variable continues to exist, it cannot be used. If the function that defines it is called again, it can continue to be used, and the value left after the previous call is saved
2. Static local variables will only be initialized once
3. Static properties can only be initialized Is a character value or a constant, expressions cannot be used. Even if a local static variable is defined without an initial value, the system will automatically assign an initial value of 0 (for numeric variables) or a null character (for character variables); the initial value of a static variable is 0.
4. When a function is called multiple times and the values ​​of certain variables are required to be retained between calls, static local variables can be considered. Although global variables can also be used to achieve the above purpose, global variables sometimes cause unexpected side effects, so it is still better to use local static variables.

Copy code The code is as follows:

function test()
{
static $var = 5; / /static $var = 1+1; will report an error
$var++;
echo $var . ' ';
}


test(); //2
test(); //3
test(); //4
echo $var; //Error: Notice: Undefined variable: var

About static global variables:

Copy code The code is as follows:

//Global variables It is a static storage method. All global variables are static variables
function static_global(){
global $glo;
$glo++;
echo $glo.'
';
}

static_global(); //1
static_global(); //2
static_global(); //3
echo $glo . '
'; //3

So static global variables are not used much.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/736831.htmlTechArticleCharacteristics of static local variables: 1. Will not change as the function is called and exits. However, although The variable continues to exist, but it cannot be used. If the function that defines it is called again...
Related labels:
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!