note 94618 added to language.variables.scope
| From: | Stewart at osu1 dot php dot net | Date: | Sat, 14 Nov 2009 22:56:28 +0000 |
| Subject: | note 94618 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162909@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 92.48.74.199
Probable Submitter: 87.194.178.55
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- https://master.php.net/note/edit/94618
Del: integrated -- https://master.php.net/note/delete/94618/integrated
Del: useless -- https://master.php.net/note/delete/94618/useless
Del: bad code -- https://master.php.net/note/delete/94618/bad+code
Del: spam -- https://master.php.net/note/delete/94618/spam
Del: non-english -- https://master.php.net/note/delete/94618/non-english
Del: in docs -- https://master.php.net/note/delete/94618/in+docs
Del: other reasons-- https://master.php.net/note/delete/94618
Reject -- https://master.php.net/note/reject/94618
Search -- https://master.php.net/manage/user-notes.php