Home > Article > Backend Development > Static variable index of PHP class
The use of static variables in PHP is relatively wide. Not only can we add the static modifier in front of a class, method or variable, we can even add the static keyword to the internal variables of the function. The value of a variable with the static modifier added will not be lost even after the function is executed. That is to say, the variable will still remember the original value the next time this function is called.
Directly upload the code
<?php class example{ public static $pa; public $pb; public function __construct(){ $this->pb = ++self::$pa; } } $a = new example; $b = new example; echo $a->pb; echo '<hr/>'; echo $b->pb; ?>
Originally I thought the result should be
<span>1</span> ----------------------------------------------------- <span>1</span>
But I was wrong, The correct result is
<span>1</span> ---------------------------------------------------------------------------------- <span>2</span>
If you haven’t learned the basics well, hurry up and catch up.
The above introduces the static variable index of the PHP class, including PHP static variables and index content. I hope it will be helpful to friends who are interested in PHP tutorials.