Home >Backend Development >PHP Problem >The difference between php static variables and variables

The difference between php static variables and variables

(*-*)浩
(*-*)浩Original
2019-09-25 09:55:083333browse

The difference between php static variables and variables

Ordinary variables: automatically destroyed after the function is executed;

Static variables: will not be destroyed and retain the last value

Recommended manual : php complete self-study manual

Adding static in front of a variable constitutes a static variable (static variable). (Recommended learning: PHP programming from entry to proficiency)

The difference between static global variables and ordinary global variables: static global variables are only initialized once to prevent them from being referenced in other file units;

Static has nothing to do with the three attribute formats of public, protected, and private. They are not parallel.

Public, protected, and private can also be set to static

public static $a;
private static $b;

Static variables have the following characteristics:

When a static variable is defined in a function, this variable will not be used the next time it is called even if the function exits. function, it uses the value left over from the last time it was called.

In addition, although the variable does not continue to exist when the function exits, it cannot be used outside the function.

Recommended related articles:
1.When are php static variables destroyed
2.What are the differences between static variables and global variables in PHP?
Related video recommendations:
1.Dugu Jiujian (4)_PHP video tutorial

Therefore, The application timing of static variables is as follows:

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.

The basic function of static attributes is that unlike ordinary attributes, static attributes will remember the previous value. For example:

function a() {
  $a = 1;
  $a += 1;  
  echo $a; 
}  
//然后连续3次调用这个函数测试下
a();
a();
a();
上面代码会输出 2 2 2
 
改成静态属性:
 
function a() {
  static $a = 1;
  $a += 1;  
  echo $a; 
}  
//然后连续3次调用这个函数测试下
a();
a();
a();

The above will output 2 3 4

For static variables defined in a class, to put it simply, static members of the class can be used directly without instantiation.

The above is the detailed content of The difference between php static variables and 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