Re: 2 Questions: Static variables and the nature of the online manual

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Svevo Romano schreef:
Hi there,

Many thanks for your answer. I've also gone through your example and it took
me 10 minutes to understand how the operator precedence was working there.
Was expecting 1 on the first call :)

But this is not the point. You've nailed my question very preciseley in your
first answer: 'the preceding line is only run on the first call to the
function'.

My only question is (at it is related to the nature of the online manual):
how do you know it and I don't? This thing is the only logical explanation
to the fact the $a doesn't get initialized again to 0 in any subsequent call
to the function, but it's not written anywhere in the manual page. And it
seems the most important statement in my opinion, that justifies what I see
as an exception to a normal flow.

can't remember where I picked up the meaning/working of 'static' - I think I
just worked it out by trial and error, or I read about it sometime on this list :-)

the manual does talk about statics: http://php.net/static

notice you can type 'http://php.net/<FOO>' to go straight to certain docs, replace
<FOO> with a function name, extension name, core concept, or whatever ... if nothing
is found you get a 'did you mean ....?' type page otherwise you go directly to the
relevant manual page.


Hope all this makes sense.
Thanks,
S
In 4/3/08 13:14, Jochem Maas, jochem@xxxxxxxxxxxxx ha scritto

Svevo Romano schreef:
Hello,

I got this e-mail address from the ŒAdd note¹ page within the php.net
website. I was going to post something that was a question and I realised I
was in the wrong place :)

I have 2 basic questions and I¹m sorry if they may seem too basic. I¹m a bit
new to php.

The first question has to do with the static variables. I understand how
this works from the examples, but there is something that I cannot seem to
find clearly stated anywhere on that page.

The example:

<?php
function Test()
{
    static $a = 0;
the preceding line is only run on the first call to the function.

    echo $a;
    $a++;
}
?>

Of course works (I¹ve tested it on my server), but it is still obscure to
me, according to general programming principles, since I¹m still assigning
zero (0) to $a on each call to the Test function. How does this exactly work
when the static word is found? Is there and index that keeps track of each
call to the function ignoring any assignment in subsequent calls to the
function? Why doens¹t this work when you assign an expression result to the
variable?
do something like

function Test()
{
static $a;

if (!isset($a))
$a = 0;
if ($a % 2)
$a = $a * 2;

echo $a++;
}

The second question has to do with the online manual. I¹ve found several
things on that manual specified in comments and not in the actual manual
part of it. What is the nature of the manual? Contributions from voluteers?
Is there any official manual I can buy that documents everything about the
language from the source? Or any official company that maintains the
language and that possibly offers support as well?
php.net/ is the official manual. recommended to read it in english so your
looking at the latest version (not always the case in other languages).

user notes/comments are exactly that - notes, tips, gotcha's, examples related
to whatever is documented on a given manual page. occasionally some of the
best
user notes are merged into the official documentation.

Many thanks in advance for your time.





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux