Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
Retiring, March 2020 - sorry, you have missed our final public course.
The Coronavirus situation has lead us to suspend public training - which was on the cards anyway, with no plans to resume

Please ask about private 'maintenance' training for Python, Tcl, Perl, PHP, Lua, etc
Happily continuing private consultancy / programming work
Static variables and ampersands in PHP

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++; }
  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 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)
  [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)

Back to
Curly braces within double quoted strings in PHP
Previous and next
Horse's mouth home
Forward to
How to show a large result set page by page in PHP
Some other Articles
London to and from Melksham by public transport
Why do I teach niche skills rather than mainstream?
Shipping a test harness with your class in PHP
How to show a large result set page by page in PHP
Static variables and ampersands in PHP
Curly braces within double quoted strings in PHP
An example of an injection attack using Javascript
Saturdays out from Melksham - to Oxford, to Didcot or to Swindon
On Malachite Green
Both feet on the same pavement
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2020: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2630_Sta ... n-PHP.html • PAGE BUILT: Sat May 27 16:49:10 2017 • BUILD SYSTEM: WomanWithCat