The difference between static and normal variables in PHP

Source: Internet
Author: User
Tags function examples valid

Adding static to the front of a variable constitutes a statically variable

The difference between a static variable and a normal variable is that the scope of a non-static global variable is the entire source program, and when a source program is composed of multiple source files, non-static Global variables are valid in each source file. A static global variable restricts its scope, that is, it is only valid within the source file that defines the variable, and it 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 only be common to functions within that source file, so it is possible to avoid causing errors in other source files.

The difference between a static variable and a normal variable:

Static global variables differ from ordinary global variables: Static global variables are only first made once, preventing them from being referenced in other file cells;
Static local variable and ordinary local variable difference: Static local variable is only initialized once, the next time according to the last result value;
The static function differs from the ordinary 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 before the global variable is added with the keyword static.

1 in-memory location: static storage (static storage exists throughout the program's operation)

2 initialization: Uninitialized global static variables are automatically initialized by the program to 0 (the value of the automatic object is arbitrary unless he is shown initialized)

3 scope: Global static variable is not visible outside of declaring his file. Start at the end of the file exactly as defined.

The benefits of static variables:

will not be accessed by other files, modified
Variables with the same name can be used in other files, and no conflicts will occur.
In-memory location: static storage
Initialization: Uninitialized global static variables are automatically initialized by the program to 0 (the value of an automatic object is arbitrary unless he is shown initialized)
Scope: The scope is still a local scope, and when the function or statement block that defines it ends, the scope ends.

Note: When static is used to modify a local variable, it changes the storage location of the local variable, and changes it from the original stack to a static storage area. But the local static variable, after leaving the scope, is not destroyed, but still resides in memory until the end of the program, but we can no longer access him.
When static is used to decorate a global variable, it changes the scope of the global variable (not visible outside the declaration of his file), but does not change its location or in a static storage area.
Common function Examples:

function Test () {
$w 3sky = 0;
echo $w 3sky;
$w 3sky++;
Each time the function is invoked, the value of $w 3sky is set to 0 and output "0". Adding a variable to the $w 3sky++ has no effect, because once the function is exited, the variable $w 3sky does not exist.

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

function Test ()
static $w 3sky = 0;
echo $w 3sky;
$w 3sky++;
Every call to test () of this function will output $w 3sky value and add 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, no exits. Be sure to have a way to abort recursion. The following simple function recursive count to 10, using static variables $count to determine when to stop, static variables and recursive functions examples:

? Php
function Test ()
static $count = 0;
$count + +;
Echo $count;
if ($count < 10) {
Test ();

Note: Static variables cannot be declared in expressions such as static $int = 1+2; This is wrong   static $int = 1; This is the right way to declare.

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: 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.