[PHP] 2 Questions: Static variables and the nature of the online manual
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; 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? 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? Many thanks in advance for your time.
Re: [PHP] 2 Questions: Static variables and the nature of the online manual
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. Hope all this makes sense. Thanks, S In 4/3/08 13:14, Jochem Maas, [EMAIL PROTECTED] 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.
Re: [PHP] 2 Questions: Static variables and the nature of the online manual
Just one word, Thanks :) S In 4/3/08 16:22, David Giragosian, [EMAIL PROTECTED] ha scritto I would hazard a guess that the 'static' keyword and functionality comes from ANSI C. I just pulled The C Programming Language by Kernighan and Ritchie from the book case and it is described there in it. Essential book, by the way, IMHO. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] 2 Questions: Static variables and the nature of the online manual
Hi Daniel, Many thanks to you as well. I really appreciate your effort in answering my queries guys. It means I'll do my best with books and the online manual. And today I've probably found the best resource. The community! Still, I jusy wonder how Jochem knew that the line is only executed the first time a function is called while this info is not available on the online manual. It's maybe all about how close you are to the community and how many degrees are between yourself and the source? I mean, I understand the manual is maintained by the community, but I suppose the language is developed by a small core of programmers that participate to the general discussion to some extent... I¹m just trying to figure out the shape of the landscape. I tend to start from the bigger picture before getting into details. :) Cheers In 4/3/08 16:00, Daniel Brown, [EMAIL PROTECTED] ha scritto On Tue, Mar 4, 2008 at 7:16 AM, Svevo Romano [EMAIL PROTECTED] wrote: 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? We (the PHP community) maintain the manual through registered CVS accounts. As Jochem said, php.net is the official manual - there is no official manual to buy, and no way for a company to document everything about the language. This is because, believe it or not, the language changes multiple times per day, with added functionality all the time. The best a company could do is document everything on a specific version but by the time that task is complete, the version documented would be obsolete. The comments are just posts by whomever feels like typing and submitting. It's generally a tips and tricks sort of thing, and is an excellent source, but an unofficial source.
Re: [PHP] 2 Questions: Static variables and the nature of the online manual
Ok Jochem, It makes a lot of sense. Now I know what I can expect from the manual and what kind of approach I should have. I hope to contribute as well in the future. Many thanks, S In 4/3/08 16:11, Jochem Maas, [EMAIL PROTECTED] ha scritto 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, [EMAIL PROTECTED] 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
Re: RES: [PHP] 2 Questions: Static variables and the nature of the online manual
Cheers Thiago, In fact this was going to be my next question. I think I will report a documentation bug, just because, after all the discussion we had today, I realize that this is quite a common behaviour in other languages, but for somebody new to languages, being them programming or scripting ones, the fact that the $a=0 line doesn't get executed on subsequent calls isn't so obvious. In other words, from the standpoint of someone that is relatively new to the details (at least), the flow is: 'ok, I do appreciate that the engine keeps memory of the value of that variable declared as static after the function ends, but assigning a value to it at the very beginning of the function seems like the next time the function is going to be called, it will assaign that value again and again...and again'. And I guess the manual wants to be as clear as possible, considering that the examples are often 'foo 'and '$a' related :P In other words I think that it is indeed targeted to beginners as wel, isn't it? Thanks for all the valuable info btw. :) In 4/3/08 16:59, Thiago Pojda, [EMAIL PROTECTED] ha scritto -Mensagem original- De: Svevo Romano [mailto:[EMAIL PROTECTED] 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. Hope all this makes sense. Thanks, S me You can use http://bugs.php.net/report.php to report a documentation bug and they'll change the docs :) Thiago /me -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] 2 Questions: Static variables and the nature of the online manual
Yes Richard, In fact I know ActionScript and JavaScript and I'm trying to nail the peculiarities of php. I totally agree. I am not used to use ' - ' to call a method if you know what I mean, but the rest is quite familiar, phew! :) Aside from that, this was just one of the things I could not really understand properly: I could understand the effects, not the reasons behind the effects, if it makes any sense. I think the manula is lacking a couple of lines. One funny thing: in the Welling Thomson - PHP and MySQL Web Development book, at some point this thing is mentioned. They promised they would explain the concept in full detail in chapter 5 and, in chap 5, they kinda forgot to do it.. hehe All the best. In 4/3/08 22:05, Richard Lynch, [EMAIL PROTECTED] ha scritto On Tue, March 4, 2008 10:12 am, Svevo Romano wrote: Still, I jusy wonder how Jochem knew that the line is only executed the first time a function is called while this info is not available on the online manual. It's maybe all about how close you are to the community and how many degrees are between yourself and the source? That's how it works in C. And Perl. And Modula-2. And Ada. And Pascal. And even Lisp. . . . After you've learned a couple computer languages, the rest are mostly about differences and gotchas rather than learning something new. Ok, except the Lisp/Scheme/Prolog stuff, where you have to think inside-out. :-) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php