PHPstatic local static variables and global static variables summary_php tutorial

Source: Internet
Author: User
Summary of PHPstatic local static variables and global static variables. Static local variable features: 1. it will not change with the function call and exit. However, although the variable still exists, it cannot be used. If you call the function to define it again Static local variables:

1. it will not change with the function call and exit. However, although the variable still exists, it cannot be used. If you call a function that defines it again, it can continue to be used and save the value left after the previous call.
2. static local variables will only be initialized once
3. static attributes can only be initialized as a character value or a constant, and expressions are not supported. Even if no initial value is assigned to a local static variable, the system automatically assigns the initial value 0 (for numeric variables) or null characters (for character variables). The initial value of the static variable is 0.
4. when you call a function multiple times and require that the values of some variables be retained between calls, you can consider using static local variables. Although global variables can also achieve the above purpose, global variables sometimes cause unexpected side effects, so it is best to use local static variables.

The code is as follows:


Function test ()
{
Static $ var = 5; // static $ var = 1 + 1; an error is returned.
$ Var ++;
Echo $ var .'';
}


Test (); // 2
Test (); // 3
Test (); // 4
Echo $ var; // error: Notice: Undefined variable: var

Static global variables:

The code is as follows:


// Global variables are static storage, and all global variables are static variables.
Function static_global (){
Global $ glo;
$ Glo ++;
Echo $ glo .'
';
}

Static_global (); // 1
Static_global (); // 2
Static_global (); // 3
Echo $ glo .'
'; // 3

Therefore, there are not many static global variables.

Lifecycle 1. does not change with the function call and exit. However, although the variable still exists, it cannot be used. If you call the function to define it again...

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.