The life cycle of a variable

Source: Internet
Author: User

A variable has not only its specific scope of action, but also its life cycle, the period of its survival. The life cycle of a variable is a time period in which a variable can be used, in which case the variable is valid, and once the time period variable is exceeded, we can no longer access the value of the variable.

PHP has the following provisions for the life cycle of variables.

The life cycle of a local variable is the entire process in which the function is called. When the function at which the local variable is located ends, the life cycle of the local variable ends.

The lifetime of a global variable is the entire process called by the ". Php" script file where it resides. When the script file that contains the global variable ends the call, the life cycle of the global variable ends.

Sometimes when a custom function is finished, we want the variable in the function to still exist, then we need to declare the variable as a static variable. Declare a variable as a static variable by adding the "static" keyword before the variable.

The example code for applying a static variable is as follows.

As can be seen from the results above, the value of variable a will increase by 1 each time the function test () is called. That is, the variable a still exists after each call to the function. When the function test () is called again, variable a will use the value that was obtained after the last call to the function. We can also conclude from the above example that the scope of the static variable is the same as the local variable, but the life cycle is the same as the global variable.

We can understand static variables in this way.

Only the initial value of the static variable in the function body is taken when the function is first called. When the function is called again later, the static variable takes the value that was obtained when the function was last called.

When assigning an initial value to a static variable, you cannot assign an expression to the static variable.

  

The life cycle of a variable

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.