Main Content

Static variables and ampersands in PHP

Archive - Originally posted on "The Horse's Mouth" - 2010-02-10 23:10:49 - Graham Ellis

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.