In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people don't understand the knowledge points of this article "What are the differences between static variables and ordinary variables in php", so Xiaobian summarizes the following contents for everyone. The contents are detailed, the steps are clear, and they have certain reference value. I hope everyone can gain something after reading this article. Let's take a look at this article "What are the differences between static variables and ordinary variables in php".
Static variables are initialized only once, while ordinary variables can be initialized multiple times. The scope of a common global variable is the entire source program. When a source program consists of multiple source files, a common global variable is valid in each source file; a static global variable is valid only in the source file that defines the variable.
Operating environment of this tutorial: Windows 7 system, PHP7.1 version, DELL G3 computer
Adding static to the variable in front of it constitutes 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. A static global variable is limited in scope, meaning it is valid only in the source file that defines it, 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 and can only be common to functions within that source file, errors can be avoided in other source files.
Difference between static variables and ordinary variables:
Static global variables are different from ordinary global variables: static global variables are initialized only once to prevent them from being referenced in other file units;
Static local variables are initialized only once, and the next time is based on the previous result value.
Static functions differ from ordinary functions: static functions have only one copy in memory, ordinary functions maintain one copy for each call.
A global variable is defined as a global static variable by adding the keyword static before it.
1) Location in memory: static storage area (static storage area exists during the entire program run)
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 explicitly initialized)
Scope: A global static variable is invisible outside the file in which it is declared. Exactly from the definition to the end of the file.
Benefits of static variables:
It will not be accessed by other files, and there will be no conflict when modifying variables that can use the same name in other files.
Location in memory: static storage
Initialization: Uninitialized global static variables are automatically initialized to 0 by the program (the value of an automatic object is arbitrary unless it is explicitly initialized)
Scope: Scope is still local and ends when the function or block defining it ends.
Note: When static is used to modify a local variable, it changes the storage location of the local variable from the original stack to static storage. But local static variables are not destroyed after they leave scope, but remain in memory until the end of the program, but we can no longer access them.
When static is used to modify a global variable, it changes the scope of the global variable (invisible outside the file in which it is declared), but does not change its location, still in static storage.
Examples of common functions:
To define the variable $w3sky as static, the code is as follows:
Static variables also provide a way to handle recursive functions. A recursive function is a method that calls itself. Be careful when you write recursive functions, because you can recurse indefinitely and there's no exit. Make sure there is a way to abort recursion. The following simple function recursively counts to 10, using the static variable $count to determine when to stop, an example of static variables versus recursive functions:
Note: Static variables cannot be declared in the form of an expression e.g. static $int = 1+2; this is wrong and static $int = 1; this declaration is correct.
The above is about the content of this article "What are the differences between static variables and ordinary variables in php". I believe everyone has a certain understanding. I hope the content shared by Xiaobian will help everyone. If you want to know more relevant knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.