The difference between static and normal variables in PHP _php instances

Source: Internet
Author: User
Tags function examples
the difference between static and normal variables in PHP

Static variables are formed by adding static to the front of the variable.

The difference between a static variable and a normal variable is that the scope of the non-static global variable is the entire source program, and when a source program consists of multiple source files, non-static global variables are valid in each source file. A static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can be common only for functions within that source file, so you avoid causing errors in other source files.

The difference between a static variable and a normal variable:

The static global variable differs from the normal global variable: the static global variable is only initialized once, preventing it from being referenced in other file units;
Static local variable differs from ordinary local variable: Static local variable is initialized only once, next time based on last result value;
The static function differs from the normal function: The static function has only one copy in memory, and the normal function maintains a copy of each call.

The global variable is defined as a global static variable by adding the keyword static before the global variable.

1) in-memory location: static storage (static storage is present during the entire program run)

2) Initialize: Uninitialized global static variable is automatically initialized to 0 by the program (the value of the automatic object is arbitrary unless he is displayed initialized)

3) Scope: Global static variables are not visible outside the declaration of his file. Start with the definition exactly to the end of the file.

The benefits of the static variable:

will not be accessed by other files, modify variables in other files that can use the same name, and no conflicts will occur.

In-memory location: Static Storage Area

Initialize: Uninitialized global static variable is automatically initialized by the program to 0 (the value of the automatic object is arbitrary unless he is displayed initialized)
Scope: Scope is still a local scope, and when the function or block of statements defining it ends, the scope ends.

Note: When static is used to modify a local variable, it changes the location where the local variable is stored and changes from the original stack to a static storage area. But the local static variable is not destroyed after it leaves the scope, but still resides in memory until the program is finished, but we can no longer access it.
When static is used to modify a global variable, it changes the scope of the global variable (which is invisible outside of declaring his file), but does not change its location or in the static store.

Examples of common functions:

<?phpfunction Test () {  $w 3sky = 0;  echo $w 3sky;  $w 3sky++;  /* The value of $w 3sky is set to 0 and output "0" each time the function is called. Adding a variable to the $w 3sky++ has no effect, because once exiting this function the variable $w 3sky does not exist. */}?>                  

To define a variable $w 3sky as static (static), the code is as follows:

<?phpfunction Test () {  static $w 3sky = 0;  echo $w 3sky;  $w 3sky++;} Each call to test () of this function outputs a value of $w 3sky and adds one.?>    

Static variables also provide a way to handle recursive functions. A recursive function is a method of calling itself. Be careful when writing recursive functions, as there may be infinite recursion and no exits. Be sure there is a way to abort recursion. The following simple function recursively counts to 10, using static variable $count to determine when to stop, static variables and recursive function examples:

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

Note: Static variables cannot be declared in the form of an expression such as: static $int = 1+2; This way is wrong and static $int = 1; This way of declaring is true.

Thank you for reading, hope to help everyone, thank you for the support of this site!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.