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

The difference between static static variables and ordinary variables in php

迷茫
Release: 2023-03-05 10:34:02
Original
1372 people have browsed it

What is the difference between static variables and ordinary variables? For many friends who are new to PHP, it may not be easy to understand. Today we will take a look at the difference between static variables and ordinary variables in PHP. Friends who need it can Refer to the difference between static static variables and ordinary variables in

#Add static in front of the variable to form a static variable (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 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 used in other file units Referenced;
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: there is only one copy of the static function in the memory. Ordinary functions maintain a copy each 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 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 examples:

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

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

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

Static variables also provide a Ways to handle 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--;
}
?>
Copy after login

Note: Static variables cannot be declared as expressions. For example: static $int = 1+2; is wrong, but static $int = 1; is correct.

Thank you 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