PHP variable types and scope of the detailed

Source: Internet
Author: User
Tags php class

  What is the scope of a variable? The scope of a variable refers to the valid range of variables within a lifetime of a script. Generally, there are global and local points

The scope of variables in PHP can be divided into: Super Global (the special type of global variables, in the local scope can be directly used), global, Local, static (is a special type of local variables) in PHP, the global variable is actually a static global variable, if not unset explicit release, So the script runs and ends global variables are released. Local static variable subdivision can be a local static function variable (a static variable declared in a function), a local static member variable (a static property declared in a class that is shared by all class instances) a local static variable that is automatically freed only when the script is run over   Super Global variables: In any scope of a script can be accessed, these are PHP built-in     code as follows: $GLOBALS $_server $_get $_post $_files $_session (persistent storage) $_ COOKIE (persistent storage) $_request $_env global variable: The declared variable is not inside a language structure such as class,function,if, if it is to be used inside languages such as class,function,if, Need to use keyword Global or Super global variable $globals   Static variables: variables declared with a keyword static in a function, the value of a static variable remains until the script ends a local variable: in class,function,if/while/ For such structure statements internal declarations of variables   1.global keywords and $globals instance code are as follows: <?php $a = 0; function foo () {    global $a;     echo $a;} function Foo2 () {    echo $GLOBALS [' a ']; 2 . static variables and ordinary local variables distinguish the instance code as follows: <?php function foo1 () {    $var = 0;     $var + +     return $ var Echo Foo1 (); Echo Foo1 (); Output is 1 function foo () {    static $var = 0;     $var + +;     return var;Foo (); echo foo (); First Output 1 second time 2 3.static keywords can also declare static properties and static methods static properties are only called by the class, but not by the class instance call static methods can not use $this, you can only access the static properties of the class with self   Another part of the code that understands the static variable:     code is as follows: <?php class T {        $v = = =         Public Function A ()         {                static $v AR = 10;                 $var + +;                 echo $var. "<br>n";        }         public static function AA ()         {                self:: $v + +;                 echo self:: $v. "<br>n";        }} $o 1 = new T (); $o 1->a ()//output $o 2 = new T (); $o 2->a ()//Output T::AA ()//Output one $o 1->AA ()//Output $o 2->AA ();//output   from the above code, we know that if there are static variables in a class member method, Even for different class instances, they share this static variable, although this static variable is not a class static member variable, this is easy to confuse.  

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.