What is the role of php static variables?

青灯夜游
Release: 2023-02-27 10:16:01
Original
3315 people have browsed it

What is the role of php static variables?

What are static variables?

Static variable The type specifier is static.

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. (It is not released during the entire running of the program), and it can also be considered that its memory address remains unchanged until the entire program ends.

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

As long as the keyword static is added before the variable, the variable becomes a static variable.

The role of php static variables

1. Modify variables inside the function. Static variables maintain their value while the function is called.

<?php
function testStatic() {
    static $val = 1;
    echo $val."<br />";;
    $val++;
}
testStatic();   //output 1
testStatic();   //output 2
testStatic();   //output 3
?>
Copy after login

Program running result:

1
2
3
Copy after login

2. Modify attributes or methods in the class.

Modified attributes or methods can be accessed through the class name. If the modified attribute is a class attribute, the value is retained.

<?php
class Person {
    static $id = 0;
 
    function __construct() {
        self::$id++;
    }
 
    static function getId() {
        return self::$id;
    }
}
echo Person::$id;   //output 0
echo "<br/>";
 
$p1=new Person();
$p2=new Person();
$p3=new Person();
 
echo Person::$id;   //output 3
?>
Copy after login

Program running result:

0
3
Copy after login

3. In the class Modify variables in the method.

<?php
class Person {
    static function tellAge() {
        static $age = 0;
        $age++;
        echo "The age is: $age
";
    }
}
echo Person::tellAge(); //output &#39;The age is: 1&#39;
echo Person::tellAge(); //output &#39;The age is: 2&#39;
echo Person::tellAge(); //output &#39;The age is: 3&#39;
echo Person::tellAge(); //output &#39;The age is: 4&#39;
?>
Copy after login

Program running results:

The age is: 1 The age is: 2 The age is: 3 The age is: 4
Copy after login

For more PHP-related knowledge, please visit php中文网!

The above is the detailed content of What is the role of php static variables?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!