If you call a function within your program, the last thing you usually want is to have the debris from a previous call left over and effecting how it runs. The fact that you previously looked for a florist in Melksham should not effect your search for a brewer in Devizes, and asking for the average of 50 and 55 mustn't be effected by the fact that your previous average request took input on 12 and 21. So by default, variables within a function are new and fresh every time you call the function.
There are, though, some occasions where you do want a function to carry in from before, and in such a case in PHP you can declare those variables you want to carry on to be
static. If the description of your function uses the word "next" or you describe it as "iterating through" some data, you've got a clue that you may have one of the quite rare times when it'll be good practice to use one of them. Here's an example - from a new demonstration I wrote showing how data can be displayed page by page - that I wrote on the
PHP course yesterday. In this case, I've done various setup calculations the first time that the function is called, but not had to repeat them on subsequent calls because they remain unchanged.
function show_item($bits,&$matched,&$shown,$pageno,$pagesize) {
# Register variables that you want to live from one call to this function
# to the next
static $call = 0;
static $showfrom, $showto;
# First call ONLY - work out the range of matches to show
if ($call++ == 0) {
$showfrom = $pageno * $pagesize;
$showto = $showfrom + $pagesize - 1; }
# If the current matched item is in the range to show, add it to the return string
if ($matched >= $showfrom and $matched <= $showto) {
$showrow .= "<tr><td>$matched: $bits[0]</td><td>$bits[1].</td></tr>";
$shown++; }
$matched++;
return $showrow;
}
Care must be taken with this sort of function - in effect you are writing a singleton object / something that works on only one data set and it would need refactoring if you wanted (say) to display two data sets in the same page. One way to do do this would be some sort of special 'reset' call, and another would be to take in and pass back out the $call variable. You can see this source code within the context of the demonstration
[here] and you can run it
[here]. There's a further comment on static variables
[here].
Note - the & on some of the parameters means that these variables are effecting the variable passed in at that position within the calling code - in other words, the variable $matched is read / write as far as the calling code is concerned (it's changed within your function) but the variable $pagesize comes to you read only as far as the calling code is concerned, as PHP will copy it into the memory space used by the function, and any changes subsequently made would be to that local copy.
(written 2010-02-10, updated 2010-03-05)
Associated topics are indexed under
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)
[2488] A variable number of arguments in a PHP function - (2009-11-02)
[1784] Global - Tcl, PHP, Python - (2008-09-03)
[1380] Static variables in PHP - (2007-10-05)
[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
London to and from Melksham by public transportWhy do I teach niche skills rather than mainstream?Shipping a test harness with your class in PHPHow to show a large result set page by page in PHPStatic variables and ampersands in PHPCurly braces within double quoted strings in PHPAn example of an injection attack using JavascriptSaturdays out from Melksham - to Oxford, to Didcot or to SwindonOn Malachite GreenBoth feet on the same pavement