The PHP Static Statement
|The magic of the PHP static statement is that it allows a function to "remember" the value of a local variable for the next time the function is called.|
In this tutorial you will learn how to use the PHP static statement to give a PHP function a "memory". A basic PHP function does not have the ability to remember the value of a local variable that has been created or changed within the function. Once the function is finished, all knowledge of the value of the function's variable disappears. To get around this problem we can use the PHP static statement to tell the function to remember the value of a local variable from one call of a function to the next call. Let's take a look at the basic code.
function function_name( )
static $variable = value;
This is all there is to our function. The next time this function is called, the value of the $no_calls variable is remembered from the last execution of the function and then increased by one again.
This site needs an editor - click to learn more!
Editor's Picks Articles
Top Ten Articles
Content copyright © 2018 by Diane Cipollo. All rights reserved.
This content was written by Diane Cipollo. If you wish to use this content in any manner, you need written permission. Contact BellaOnline Administration for details.