Home  >  Article  >  Backend Development  >  What is the difference between static variables and ordinary variables in php

What is the difference between static variables and ordinary variables in php

青灯夜游
青灯夜游Original
2022-01-27 17:10:374354browse

Difference: 1. Static variables are only initialized once, while ordinary variables can be initialized multiple times. 2. The scope of ordinary global variables is the entire source program. When a source program consists of multiple source files, ordinary global variables are valid in each source file; while static global variables are only valid in the source file in which the variable is defined. Valid within.

What is the difference between static variables and ordinary variables in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

Add static in front of the variable to form Static variables (static variables).

The difference between static variables and ordinary variables is that the scope of non-static global variables is the entire source program. When a source program consists of multiple source files, non-static global variables are in each source file. Effective. Static global variables limit their scope, that is, they are only valid within the source file in which the variable is defined, and cannot be used in other source files of the same source program. Since the scope of static global variables is limited to one source file and can only be shared by functions in that source file, errors can be avoided in other source files.

The difference between static variables and ordinary variables:

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 ;
The difference between static local variables and ordinary local variables: static local variables are only initialized once, and the next time is based on the previous result value;
The difference between static functions and ordinary functions: static functions have only one copy in memory, ordinary functions Maintain a copy on each call.

Add the keyword static before the global variable, and the global variable is defined as a global static variable.

1) Location in memory: static storage area (static storage area exists during the entire program running)

2) Initialization: Uninitialized global static variables will be automatically initialized by the program Is 0 (the value of an automatic object is arbitrary unless it is explicitly initialized)

3) Scope: Global static variables are not visible outside the file in which they are declared. Exactly from the point of definition to the end of the file.

Benefits of static variables:

will not be accessed by other files. Variables with the same name can be used to modify other files without conflict.

Location in memory: static storage area

Initialization: Uninitialized global static variables will be automatically initialized to 0 by the program (the value of the automatic object is arbitrary , unless he is explicitly initialized)
Scope: The scope is still a local scope. When the function or statement block that defines it ends, the scope ends.

Note: When static is used to modify local variables, it changes the storage location of local variables from the original stack to the static storage area. However, the local static variable is not destroyed after leaving the scope, but still resides in the memory until the end of the program, but we can no longer access it.
When static is used to modify a global variable, it changes the scope of the global variable (not visible outside the file in which it is declared), but does not change its storage location, which is still in the static storage area. .

Ordinary function example:

<?php
function Test() {
  $w3sky = 0;
  echo $w3sky;
  $w3sky++;
  /*函数每次调用时都会将 $w3sky 的值设为 0 并输出 "0"。将变量加一的 $w3sky++ 没有其到效果,因为一旦退出本函数则变量 $w3sky 就不存在了。*/
}
?>

To define the variable $w3sky as static, the code is as follows:

<?php
function Test() {
  static $w3sky = 0;
  echo $w3sky;
  $w3sky++;
} //本函数每调用Test()都会输出 $w3sky 的值并加一。
 
?>

Static variables are also provided A way to deal with recursive functions. A recursive function is a method that calls itself. Be careful when writing recursive functions, as they may recurse indefinitely without an exit. Be sure to have a way to abort the recursion. The following simple function recursively counts to 10 and uses the static variable $count to determine when to stop. Example of static variables and recursive functions:

<?php
function Test() {
  static $count = 0;
  $count++;
  echo $count;
  if ($count < 10) {
    Test();
  }
  $count--;
}
?>

Note: Static variables cannot be declared as expressions. For example: static $int = 1 2; This way is wrong, but static $int = 1; This way of declaration is right.

Recommended learning: "PHP Video Tutorial"

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