note 95068 added to language.variables.scope
| From: | brian at pribis dot com | Date: | Fri, 11 Dec 2009 14:49:51 +0000 |
| Subject: | note 95068 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-163859@lists.php.net to get a copy of this message | ||
I'm not sure what the man means by a variable having a "single scope". At first it
appears that PHP5 is lexically scoped, but I came across the following situation:
for($i=0; $i < 10; $i++){
$count += $i;
}
echo $count;
The above code will work and $count will print out its incremented value, but you will most likely
see errors in your log file to the effect of "variable count not defined".
It turns out that variables are dynamically scoped within a function (restricted dynamic?
statically dynamic? dynamically static?). However, if you declare a variable outside of a function
you have to use the global keyword. On the other hand, using a variable that was first declared
inside a loop outside of that loop works, but raises a "silent" error (logged, but not
sent to the browser).
----
Server IP: 69.147.83.197
Probable Submitter: 66.218.6.34
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- https://master.php.net/note/edit/95068
Del: integrated -- https://master.php.net/note/delete/95068/integrated
Del: useless -- https://master.php.net/note/delete/95068/useless
Del: bad code -- https://master.php.net/note/delete/95068/bad+code
Del: spam -- https://master.php.net/note/delete/95068/spam
Del: non-english -- https://master.php.net/note/delete/95068/non-english
Del: in docs -- https://master.php.net/note/delete/95068/in+docs
Del: other reasons-- https://master.php.net/note/delete/95068
Reject -- https://master.php.net/note/reject/95068
Search -- https://master.php.net/manage/user-notes.php