Home > Backend Development > PHP Tutorial > The difference between static static variables and ordinary variables in php_php example

The difference between static static variables and ordinary variables in php_php example

WBOY
Release: 2023-03-03 06:54:02
Original
1103 people have browsed it

The difference between static static variables and ordinary variables in php

Add static in front of a variable to form a static variable.

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 valid in each source file. 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, while ordinary functions maintain a copy every time they are called.

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 to 0 by the program (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:

It will not be accessed by other files. You can modify variables with the same name in 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 an automatic object is arbitrary unless it 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 (it is 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:

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

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

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

&#63;>    

Copy after login

Static variables also provide 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, using the static variable $count to determine when to stop. Example of static variables and recursive functions:

<&#63;php
function Test() {
  static $count = 0;
  $count++;
  echo $count;
  if ($count < 10) {
    Test();
  }
  $count--;
}
&#63;>
Copy after login

Note: Static variables cannot be declared in the form of expressions. For example: static $int = 1+2; This way is wrong, but static $int = 1; This way of declaration is correct.

Thanks for reading, I hope it can help you, thank you for your support of this site!

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