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."/> 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.">
Home >Backend Development >PHP Tutorial >static static variables in PHP
<code><span><span><?php</span><span><span>function</span><span>test</span><span>()</span> {</span><span>static</span><span>$nm</span> = <span>1</span>; <span>$nm</span> *= <span>2</span>; <span>print</span><span>$nm</span>.<span>"\n"</span>; } <span>// 第一次执行,$nm = 2</span> test(); <span>// 第一次执行,$nm = 4</span> test(); <span>// 第一次执行,$nm = 8</span> test(); <span>?></span></span></code>
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.
One thing to note here is that the variable assignment operation will only be called when the variable is initialized for the first time. This operation will not be called during subsequent function executions.
The above introduces the static variables in PHP, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.