|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2008-11-07 11:16 UTC] vrana@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 07 23:00:01 2026 UTC |
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.