Static variable usage in PHP, local static variable and global static variable difference

Source: Internet
Author: User
Static variables exist only in the scope of the function, static variables only live in the stack, the next time you call this function, the value of the variable will be preserved

Static variables exist only within the scope of the function, and static variables live only on the stack. General function variables are released after the function ends, such as local variables, but static variables do not. The value of the variable is preserved the next time the function is called.

Basic usage of static variables

1. Defining static variables in a class
[Access modifier] static $ variable name;
2. How to access static variables
If there are two methods to access in a class self::$ static variable name, class Name:: $ static variable name
If you are accessing outside the class: There is a method class name:: $ static variable name

Example

Class child{public $name;//This defines and initializes a static variable $nums public static $nums =0; function construct ($name) {$this->name= $name; } public Function Join_game () {//self:: $nums use static variable self:: $nums +=1; Echo $this->name. " Added snowmen game "; }}//created three children $child 1=new child ("Li Kui"); $child 1->join_game (); $child 2=new Child ("Zhang Fei"); $child 2->join_game (); $child 3=new Child ("Tang priest"); $child 3->join_game (); See how many people play the game echo "<br/> have this". Child:: $nums;

Characteristics of static local variables:

1. It does not change with the invocation and exit of the function, although the variable continues to exist but cannot be used. If the function that defines it is called again, it can continue to be used, and the value left after the previous call is saved
2. Static local variables are initialized only once
3. A static property can only be initialized to a character value or a constant and cannot be used with an expression. Even if the local static variable is defined without an initial value, the system automatically assigns the initial values of 0 (to numeric variables) or null characters (to character variables), and the static variable is initially 0.
4. Consider using static local variables when you call a function multiple times and require the values of certain variables to be preserved between calls. Although this can be achieved with global variables, global variables sometimes cause unintended side effects, so it is advisable to use local static variables.

function test () {    static $var = 5;  Static $var = +, will error    $var + +;    Echo $var. ' ';} Test (); 2test (); 3test (); 4echo $var; Error: notice:undefined Variable:var

About static global variables:

The global variable itself is the static storage mode, all global variables are static variable function Static_global () {    global $glo;    $glo + +;    echo $glo. ' <br> ';} Static_global (); 1static_global (); 2static_global (); 3echo $glo. ' <br> '; 3

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.