note 77693 deleted from language.variables.scope by danbrown
| From: | danbrown@php.net | Date: | Mon, 17 Oct 2011 16:22:54 +0000 |
| Subject: | note 77693 deleted from language.variables.scope by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-183929@lists.php.net to get a copy of this message | ||
Note Submitter: daniel at nohair dot com
----
Ah, nested functions. Thanks for your notes below, search on page for "nested functions"
folks. This is how this seems to work.
The child function is seen at global level only after they have been seen once. But, variables
inside functions are only reachable within the functions scope.
<?php
$var1 = "This is \$var1 OUTSIDE parent function <br />";
function parent_function() {
echo "Now inside parent <br />";
$var1 = "This is \$var1 INSIDE parent function <br />";
$var2 = "This is \$var2 INSIDE parent function <br />";
function child_function() {
echo "now inside child <br />";
//global $var1; //Calls var1 outside parent_function;
echo $var1; //doesn't work without global;
// even if we comment out $var1 outside parent function.
// global $var1 doesn't reach the one inside parent function.
echo $var2; //doesn't work; Can't seem to reach parent variables.
}
echo "Now calling child<br />";
//child_function(); //works
}
// child_function(); //causes fatal error: call to undefined function;
parent_function(); //works;
child_function(); //now works;
?>
[EDIT BY danbrown AT php DOT net: The author is referring to http://php.net/manual/en/language.variables.scope.php#52148
and http://php.net/manual/en/language.variables.scope.php#20407
]