The functions of the static keyword in php are: 1. Put it inside the function to modify the variable, and the variable value will still be saved after the function is executed; 2. Put it in the class to modify the attributes or methods. If the modification is of a class attribute, the value is retained; 3. Modify the variable in the method of the class; 4. Modify the variable in the global scope.
The functions of the static keyword are as follows:
1. Put it inside the function to modify the variable;
2. Put it in the class to modify the attributes or methods;
3. Put it in the class method to modify the variables;
4. Modify variables in the global scope; the different meanings represented by the
keywords are as follows:
1. After the function is executed, the variable value is still saved
As shown below:
<?php function testStatic() { static $val = 1; echo $val; $val++; } testStatic(); //output 1 testStatic(); //output 2 testStatic(); //output 3 ?>
2. Modified attributes or methods can be accessed through the class name. If the modified attribute is a class attribute, the reserved value
is as follows:
<?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 ?>
3. The variables in the method of modifying the class
are as follows:
<?php class Person { static function tellAge() { static $age = 0; $age++; echo "The age is: $age "; } } echo Person::tellAge(); //output 'The age is: 1' echo Person::tellAge(); //output 'The age is: 2' echo Person::tellAge(); //output 'The age is: 3' echo Person::tellAge(); //output 'The age is: 4' ?>
4. The variables that modify the global scope have no practical meaning
are as follows: Display:
<?php static $name = 1; $name++; echo $name; ?> 另外:考虑到PHP变量作用域 <?php include 'ChromePhp.php'; $age=0; $age++; function test1() { static $age = 100; $age++; ChromePhp::log($age); //output 101 } function test2() { static $age = 1000; $age++; ChromePhp::log($age); //output 1001 } test1(); test2(); ChromePhp::log($age); //outpuut 1 ?>
It can be seen that these three variables do not affect each other. In addition, there are only global scope and function scope in PHP, not block scope.
If you want to learn more related knowledge, welcome to visit php Chinese website.
The above is the detailed content of What is the function of static keyword in php. For more information, please follow other related articles on the PHP Chinese website!

The article compares ACID and BASE database models, detailing their characteristics and appropriate use cases. ACID prioritizes data integrity and consistency, suitable for financial and e-commerce applications, while BASE focuses on availability and

The article discusses securing PHP file uploads to prevent vulnerabilities like code injection. It focuses on file type validation, secure storage, and error handling to enhance application security.

Article discusses best practices for PHP input validation to enhance security, focusing on techniques like using built-in functions, whitelist approach, and server-side validation.

The article discusses strategies for implementing API rate limiting in PHP, including algorithms like Token Bucket and Leaky Bucket, and using libraries like symfony/rate-limiter. It also covers monitoring, dynamically adjusting rate limits, and hand

The article discusses the benefits of using password_hash and password_verify in PHP for securing passwords. The main argument is that these functions enhance password protection through automatic salt generation, strong hashing algorithms, and secur

The article discusses OWASP Top 10 vulnerabilities in PHP and mitigation strategies. Key issues include injection, broken authentication, and XSS, with recommended tools for monitoring and securing PHP applications.

The article discusses strategies to prevent XSS attacks in PHP, focusing on input sanitization, output encoding, and using security-enhancing libraries and frameworks.

The article discusses the use of interfaces and abstract classes in PHP, focusing on when to use each. Interfaces define a contract without implementation, suitable for unrelated classes and multiple inheritance. Abstract classes provide common funct


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver Mac version
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment