PHP variable types and scopes

Source: Internet
Author: User
What is the scope of a variable? The scope of a variable refers to the valid range of the variable within one lifecycle of the script. In general, there are global and local differences. in PHP, the scope of variables can be divided into: Super Global (a special type of global variables, which can be directly used in a local range), global, local, static (special type of local variables)
In PHP, global variables are actually static global variables. if you do not need unset explicit release, the global variables will be released only after the script finishes running.
Local static variable subdivision can be a local static function variable (static variable declared in the function), a local static member variable (static attribute declared in the class, shared by all class instances)
Local static variables will be automatically released only when the script stops running.

Super global variables: can be accessed in any scope of a script. these are built-in PHP variables.
The code is as follows:
$ GLOBALS
$ _ SERVER
$ _ GET
$ _ POST
$ _ FILES
$ _ SESSION (persistent storage)
$ _ COOKIE (persistent storage)
$ _ REQUEST
$ _ ENV
Global variables: declared variables are not in the class, function, if, or other language structures. if you want to use them within the class, function, if, or other languages, you need to use the keyword global or Super global variable $ GLOBALS.

Static variables: variables declared using the keyword static in the function. the values of static variables are retained until the script ends.
Local variables: variables declared in structure statements such as class, function, if/while/

1. global keywords and $ GLOBALS instances
The code is as follows: $ A = 0;
Function foo ()
{
Global $;
Echo $;
}
Function foo2 ()
{
Echo $ GLOBALS ['A'];
}
2. differences between static variables and common local variables
The code is as follows: Function foo1 ()
{
$ Var = 0;
$ Var ++;
Return $ var;
}
Echo foo1 ();
Echo foo1 ();
// The output value is 1.
Function foo ()
{
Static $ var = 0;
$ Var ++;
Return var;
}
Echo foo ();
Echo foo ();
// First output 1 second 2
3. static keywords can also declare static attributes and static methods
Static attributes can only be called by classes, but cannot be called by class instances.
$ This cannot be used in static methods. only static attributes of the self-defined class can be used.

In addition, a piece of code for understanding static variables:
The code is as follows:
Class t
{
Static $ v = 10;
Public function ()
{
Static $ var = 10;
$ Var ++;
Echo $ var ."
\ N ";
}
Public static function aa ()
{
Self: $ v ++;
Echo self: $ v ."
\ N ";
}
}
$ O1 = new t ();
$ O1-> a (); // output 11
$ O2 = new t ();
$ O2-> a (); // output 12
T: aa (); // output 11
$ O1-> aa (); // output 12
$ O2-> aa (); // output 13

From the code above, we can see that if a class member method contains static variables, even for different class instances, they will share this static variable, although this static variable is not a class static member variable, this is confusing.

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.