| |||||||||||
| |||||||||||
Static variables in PHP
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: <?phpAnd here are the results: What's for dinner - seagull?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. Learn about it on a course run here. (written 2007-10-05 07:46:07) Associated topics are indexed under H105 - PHP - Functions
Some other Articles
An email update for past guests and delegatesMonitoring mod_jk and how it is load balancing First Great Western - information for customers Using a MySQL database to control mod_rewrite via PHP Static variables in PHP Simple page password protection - PHP Etag in http headers - what is it? Load Balancing with Apache mod_jk (httpd/Tomcat) Choosing between mod_proxy and mod_rewrite Python v Ruby 1637 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 at 50 posts per pageThis 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). |
| ||||||||||
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho | |||||||||||