note 95068 deleted from language.variables.scope by danbrown
| From: | danbrown@php.net | Date: | Fri, 11 Dec 2009 16:19:48 +0000 |
| Subject: | note 95068 deleted from language.variables.scope by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-163860@lists.php.net to get a copy of this message | ||
Note Submitter: brian at pribis dot com
----
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).