


Usage of static variables in php, detailed explanation of the difference between local static variables and global static variables
staticVariables only exist within the function scope. Static variables only live in the stack. The next time this function is called, the variable The value will be retained
Static variables only exist within the function scope, and static variables only live on the stack. Generally, variables within functions will be released after the function ends, such as local variables, but static variables will not. The next time this function is called, the value of the variable will be retained.
Basic usage of static variables
1. Define static variables in the class
[Access modifier] static $Variable name ;
2. How to access static variables
If accessed in a class, there are two methods self::$static variable name, classname::$static variable name
If accessed outside the class: There is one method Class name::$static variable name
Example
class Child{ public $name; //这里定义并初始化一个静态变量 $nums public static $nums=0; function construct($name){ $this->name=$name; } public function join_game(){ //self::$nums 使用静态变量 self::$nums+=1; echo $this->name."加入堆雪人游戏"; } } //创建三个小孩 $child1=new Child("李逵"); $child1->join_game(); $child2=new Child("张飞"); $child2->join_game(); $child3=new Child("唐僧"); $child3->join_game(); //看看有多少人玩游戏 echo "<br/> 有这".Child::$nums;
Characteristics of static local variables:
1. Will not follow function calls and Changes occur after exiting , but 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
2. Static local variables will only be initialized once
3. StaticAttribute can only be initialized to a character value or a constant, and expressions cannot be used. 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.
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, so it is still better to use local static variables.
function test() { static $var = 5; //static $var = 1+1;就会报错 $var++; echo $var . ' '; } test(); //2 test(); //3 test(); //4 echo $var; //报错:Notice: Undefined variable: var
About static global variables:
//全局变量本身就是静态存储方式,所有的全局变量都是静态变量 function static_global(){ global $glo; $glo++; echo $glo.'<br>'; } static_global(); //1 static_global(); //2 static_global(); //3 echo $glo . '<br>'; //3
The above is the detailed content of Usage of static variables in php, detailed explanation of the difference between local static variables and global static variables. For more information, please follow other related articles on the PHP Chinese website!

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 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
