#44384 [NEW]: Static variables not very clearly explained

From: Date: Sun, 09 Mar 2008 21:20:13 +0000
Subject: #44384 [NEW]: Static variables not very clearly explained
Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-586@lists.php.net to get a copy of this message
From: svevo at svevoromano dot it Operating system: OSX PHP version: Irrelevant PHP Bug Type: Documentation problem Bug description: Static variables not very clearly explained Description: ------------ Given the following example: <?php function Test() { static $a = 0; echo $a; $a++; } ?> I reckon the explanation is lacking one important point: the fact that the static variable line is executed only on the first call to the function (at least this is what emerged in a discussion I had with some of you in the mailing list). I think this point should be clearly stated because beginners may not know how this works in C or other languages. At the moment the exlanation reads: This function is quite useless since every time it is called it sets $a to 0 and prints "0". The $a++ which increments the variable serves no purpose since as soon as the function exits the $a variable disappears. To make a useful counting function which will not lose track of the current count, the $a variable is declared static: Now, every time the Test() function is called it will print the value of $a and increment it. A non-expert could easily think that this variable gets set to zero on each call to the function. -- Edit bug report at http://bugs.php.net/?id=44384&edit=1 -- Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=44384&r=trysnapshot52 Try a CVS snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=44384&r=trysnapshot53 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=44384&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=44384&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=44384&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=44384&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=44384&r=needscript Try newer version: http://bugs.php.net/fix.php?id=44384&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=44384&r=support Expected behavior: http://bugs.php.net/fix.php?id=44384&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=44384&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=44384&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=44384&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=44384&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=44384&r=dst IIS Stability: http://bugs.php.net/fix.php?id=44384&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=44384&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=44384&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=44384&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=44384&r=mysqlcfg

« previous php.doc.bugs (#586) next »