Differences between static and common variables in php _ php instance

Source: Internet
Author: User
What is the difference between static variables and common variables? many php beginners may not understand what they click, 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:

<? Phpfunction Test () {$ w3sky = 0; echo $ w3sky; $ w3sky ++;/* The function sets the value of $ w3sky to 0 and outputs "0" for each call ". Adding $ w3sky ++ to the variable has no effect, because the variable $ w3sky does not exist once the function is exited. */}?>

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

<? Phpfunction Test () {static $ w3sky = 0; echo $ w3sky; $ w3sky ++;} // each time this function calls Test () the $ w3sky value will be output and followed by one.?>

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:

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

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!

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.