Can static local variables in php be changed?

(*-*)浩
Release: 2023-02-26 13:14:01
Original
5166 people have browsed it

The static local variables of php do not mean that they cannot change the value. The amount that cannot change the value is called a constant. The value it holds is mutable, and it will remain up-to-date. It is said to be static because it does not change when the function is called or exits. That is, if we assign a certain value to a static variable the last time the function is called, the value will remain unchanged the next time the function is called.

Can static local variables in php be changed?

Static variable The type specifier is static. (Recommended learning: PHP video tutorial)

Static variables belong to the static storage method, and their storage space is the static data area in the memory (storage units are allocated in the static storage area) , the data in this area occupies these storage spaces throughout the running of the program (not released during the entire running of the program), and can also be considered that its memory address remains unchanged until the end of the entire program (in contrast, auto automatic variables, That is, dynamic local variables belong to the dynamic storage category, occupy dynamic storage space, and are released after the function call is completed).

Although static variables always exist during the entire execution of the program, they cannot be used outside its scope.

Static local variables:

1. Internal variables of static type are the same as auto automatic variables (that is, local variables without static declaration) and are a specific function. A local variable, that is, the variable can only be used within the function in which the variable is defined. The scope of the two is the same;

The difference between the two is that the auto automatic variable will exist as the function is called and exits. Disappear, but static class local variables will not. It will always exist regardless of whether the function in which it is located is called;

However, although the variable continues to exist, it 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. In other words, an internal variable of static type is a variable that can only be used in a specific function, but always occupies storage space.

2. If initialization is performed while defining static variables in the function body, the program will no longer perform initialization operations in the future (the static variable initialization statements of basic types that appear inside the function are only activated for the first time). implement). Assigning the initial value to the automatic variable is performed when the function is called. Each time the function is called, the initial value is given again, which is equivalent to executing an assignment statement.

3. The initialization expression of static local variables must be a constant or constant expression. Even if a local static variable is defined without an initial value, the system will automatically assign an initial value of 0 (for numeric variables) or a null character (for character variables); the initial value of a static variable is 0. For the automatic variable auto, if no initial value is assigned, its value will be an uncertain value.

4. When a function is called multiple times and the values ​​of certain variables are required to be retained between calls, static local variables can be considered. Although global variables can also be used to achieve the above purpose, global variables sometimes cause unexpected side effects (mainly caused by the scope of the variable), so it is still better to use local static variables.

Note: Local static variables take up a long time in memory and have poor readability. Therefore, try to avoid using local static variables unless necessary.

The above is the detailed content of Can static local variables in php be changed?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template