Home Backend Development PHP Tutorial Usage of static variables in php, detailed explanation of the difference between local static variables and global static variables

Usage of static variables in php, detailed explanation of the difference between local static variables and global static variables

Jun 30, 2017 am 11:23 AM
php local usage

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;
Copy after login

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 . &#39; &#39;;
}
 
test(); //2
test(); //3
test(); //4
echo $var; //报错:Notice: Undefined variable: var
Copy after login

About static global variables:

//全局变量本身就是静态存储方式,所有的全局变量都是静态变量
function static_global(){
    global $glo;
    $glo++;
    echo $glo.&#39;<br>&#39;;
}
static_global(); //1
static_global(); //2
static_global(); //3
echo $glo . &#39;<br>&#39;; //3
Copy after login



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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

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

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

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

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

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

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

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

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

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

See all articles