php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #44384 Static variables not very clearly explained
Submitted: 2008-03-09 21:20 UTC Modified: 2008-11-07 11:16 UTC
From: svevo at svevoromano dot it Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: OSX
Private report: No CVE-ID: None
 [2008-03-09 21:20 UTC] 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.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-07 11:16 UTC] vrana@php.net
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."
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 23:00:01 2026 UTC