note 94618 deleted from language.variables.scope by danbrown
| From: | danbrown@php.net | Date: | Fri, 18 Dec 2009 15:42:32 +0000 |
| Subject: | note 94618 deleted from language.variables.scope by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-164111@lists.php.net to get a copy of this message | ||
Note Submitter: Stewart
----
A few people have stated already that global doesn't work in a nested function to access
variables of the outer function. However, nobody seems to know why, or even whether there's a
way to do it.
After experimenting, I've discovered that it can't be done for this reason: nested
functions in PHP aren't really nested functions at all.
Rather, all functions are in global scope. A function defined within a function is really just a
statement that, on execution, adds the function to the global scope.
To see what I mean, try this at home:
<?php
function qwert() {
yuiop();
function yuiop() {
echo 'yuiop';
}
yuiop();
}
yuiop();
qwert();
yuiop();
?>
Now delete the one call to yuiop that triggers an error and try again. Repeat until it runs without
error.