When you call a function, you typically want a nice clean start with no "leftovers" from the previous call. And that's what you get by default in PHP. However, there can be exceptions where you want data to be retained and you can do that using:
EITHER a
global variable which is shared with the main PHP script and retained throughout its run
OR a
static variable which is retained by the function while your script is running, but is NOT shared with the main code.
The typical use of a static variable is in what we call an
iterator - a function which steps through a series of results, generating the
next value in the sequence each time it's called. Here is a code example:
<?php
$walrus = 77;
function nextmeal () {
static $walrus = 0;
$munch = array("fish","seagull","penguin","ice");
$walrus++;
$walrus %= count($munch);
return $munch[$walrus];
}
for ($k=0; $k<10; $k++) {
print "What's for dinner - ".nextmeal()."?<br />";
}
print $walrus; # Just to show you this is a different walrus!
?>
And here are the results:
What's for dinner - seagull?
What's for dinner - penguin?
What's for dinner - ice?
What's for dinner - fish?
What's for dinner - seagull?
What's for dinner - penguin?
What's for dinner - ice?
What's for dinner - fish?
What's for dinner - seagull?
What's for dinner - penguin?
77
You'll note that our Walrus gets a different meal each day, because the static variable
$walrus within the
nextmeal function is retained. But (just as a demonstration) the
$walrus variable in the main code is set to 77 and remains at that value.
Full source code
here and run it
here. Further example
[here]. Learn about it on
a course run
here.
(written 2007-10-05, updated 2010-12-31)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H105 - PHP - Functions [3026] Coding efficiency - do not repeat yourself! - (2010-11-02)
[2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
[2737] Improving your function calls (APIs) - General and PHP - (2010-04-24)
[2682] Adding extensions to PHP Open Source applications - callbacks - (2010-03-17)
[2630] Static variables and ampersands in PHP - (2010-02-10)
[2488] A variable number of arguments in a PHP function - (2009-11-02)
[1784] Global - Tcl, PHP, Python - (2008-09-03)
[1357] Clean my plate, but keep my wine bottle. (PHP; Static) - (2007-09-18)
[1267] is there a lookup function in php? - (2007-07-15)
[1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[1021] PHP - static declaration - (2007-01-04)
[936] Global, Superglobal, Session variables - scope and persistance in PHP - (2006-11-21)
[866] A lazy programmer is a good programmer - (2006-09-15)
[775] Do not duplicate your code - (2006-06-23)
[421] Don't repeat code - use loops or functions - (2005-08-21)
[409] Functions and commands with dangerous names - (2005-08-11)
[340] Code and code maintainance efficiency - (2005-06-08)
[339] Passing information into and out of PHP functions - (2005-06-07)
[308] Call by name v call by value - (2005-05-11)
[223] There is a function in PHP to do that - (2005-02-21)
[96] Variable Scope - (2004-10-22)
Some other Articles
An email update for past guests and delegatesMonitoring mod_jk and how it is load balancingFirst Great Western - information for customersUsing a MySQL database to control mod_rewrite via PHPStatic variables in PHPSimple page password protection - PHPEtag in http headers - what is it?Load Balancing with Apache mod_jk (httpd/Tomcat)Choosing between mod_proxy and mod_rewritePython v Ruby