Measure the test taker's knowledge about the differences between static variables and common variables in php. What are the differences between static variables and common variables? many php beginners may not understand the differences, today, let's take a look at the differences between static and common variables in php. For more information, see
Differences between static and common variables in php
Adding static before a variable constitutes a static variable ).
The difference between static variables and common variables is that the scope of non-static global variables is the entire source program. when a source program is composed of multiple source files, non-static global variables are valid in each source file. The static global variable limits its scope, that is, it is valid only in the source file defining the variable, and cannot be used in other source files of the same source program. Because the scope of static global variables is limited to one source file, they can only be shared by functions in the source file. Therefore, errors in other source files can be avoided.
Differences between static variables and common variables:
Differences between static global variables and common global variables: static global variables are modified only once to prevent being referenced in other file units;
Differences between static local variables and common local variables: static local variables are initialized only once, and the next time is based on the previous result value;
Difference between a static function and a common function: a static function has only one copy in the memory, and a normal function maintains one copy in each call.
When the keyword static is added before the global variable, the global variable is defined as a global static variable.
1) location in the memory: Static storage area (the static storage area exists throughout the program)
2) Initialization: uninitialized global static variables will be automatically initialized to 0 by the program (the value of the automatic object is arbitrary unless it is displayed as initialized)
3) scope: The Global Static variable is invisible outside the file that declares it. Accurately starts from the 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 zone
Initialization: uninitialized global static variables will be automatically initialized to 0 by the program (the value of the automatic object is arbitrary unless it is displayed as 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 zone. However, after leaving the scope, the local static variables are not destroyed, but still resident in the memory until the program ends, but we cannot access them any more.
When static is used to modify a global variable, it changes the scope of the global variable (invisible outside the declared file), but does not change its storage location, or in the static storage area.
Common function instance:
To define the variable $ w3sky as static, the code is as follows:
Static variables also provide a method for processing recursive functions. A recursive function is a method that calls itself. Be careful when writing recursive functions, because infinite recursion may occur without exit. make sure there are methods to stop recursion. Here is an example of a simple function that recursively counts to 10 and uses the static variable $ count to determine when to stop. static variables and recursive functions:
Note: static variables cannot be declared using expressions such as: static $ int = 1 + 2. this method is incorrect, but static $ int = 1. this method is correct.
Thank you for reading this article. I hope it will help you. thank you for your support for this site!