Home  >  Article  >  Backend Development  >  Usage of static variables in php, detailed explanation of the difference between local static variables and global static variables

Usage of static variables in php, detailed explanation of the difference between local static variables and global static variables

伊谢尔伦
伊谢尔伦Original
2017-06-30 11:23:091592browse

staticVariables only exist within the function scope. Static variables only live in the stack. The next time this function is called, the variable The value will be retained

Static variables only exist within the function scope, and static variables only live on the stack. Generally, variables within functions will be released after the function ends, such as local variables, but static variables will not. The next time this function is called, the value of the variable will be retained.

Basic usage of static variables

1. Define static variables in the class
[Access modifier] static $Variable name ;
2. How to access static variables
If accessed in a class, there are two methods self::$static variable name, classname::$static variable name
If accessed outside the class: There is one method Class name::$static variable name

Example

class Child{ 
public $name; 
//这里定义并初始化一个静态变量 $nums 
public static $nums=0; 
function construct($name){ 
$this->name=$name; 
} 
public function join_game(){ 
//self::$nums 使用静态变量 
self::$nums+=1; 
echo $this->name."加入堆雪人游戏"; 
} 


}

//创建三个小孩 

$child1=new Child("李逵"); 
$child1->join_game(); 
$child2=new Child("张飞"); 
$child2->join_game(); 
$child3=new Child("唐僧"); 
$child3->join_game(); 

//看看有多少人玩游戏 
echo "<br/> 有这".Child::$nums;

Characteristics of static local variables:

1. Will not follow function calls and Changes occur after exiting , but 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. StaticAttribute can only be initialized to a character value or a constant, and 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.

function test()
{
    static $var = 5;  //static $var = 1+1;就会报错
    $var++;
    echo $var . &#39; &#39;;
}
 
test(); //2
test(); //3
test(); //4
echo $var; //报错:Notice: Undefined variable: var

About static global variables:

//全局变量本身就是静态存储方式,所有的全局变量都是静态变量
function static_global(){
    global $glo;
    $glo++;
    echo $glo.&#39;<br>&#39;;
}
static_global(); //1
static_global(); //2
static_global(); //3
echo $glo . &#39;<br>&#39;; //3



The above is the detailed content of Usage of static variables in php, detailed explanation of the difference between local static variables and global static variables. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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