#44384 [Opn->Csd]: Static variables not very clearly explained
| From: | vrana@php.net | Date: | Fri, 07 Nov 2008 11:16:24 +0000 |
| Subject: | #44384 [Opn->Csd]: Static variables not very clearly explained | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-1421@lists.php.net to get a copy of this message | ||
ID: 44384
Updated by: vrana@php.net
Reported By: svevo at svevoromano dot it
-Status: Open
+Status: Closed
Bug Type: Documentation problem
Operating System: OSX
PHP Version: Irrelevant
New Comment:
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation
better.
"Now, $a is initialized only in first call of function and every time
the test() function is called it will print the value of $a and
increment it."
Previous Comments:
------------------------------------------------------------------------
[2008-03-09 21:20:13] svevo at svevoromano dot it
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 this bug report at http://bugs.php.net/?id=44384&edit=1