note 77693 added to language.variables.scope
| From: | daniel at nohair dot com | Date: | Sun, 09 Sep 2007 08:01:49 +0000 |
| Subject: | note 77693 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130323@lists.php.net to get a copy of this message | ||
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;
?>
----
Server IP: 69.147.83.197
Probable Submitter: 72.25.87.91
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- https://master.php.net/note/edit/77693
Del: integrated -- https://master.php.net/note/delete/77693/integrated
Del: useless -- https://master.php.net/note/delete/77693/useless
Del: bad code -- https://master.php.net/note/delete/77693/bad+code
Del: spam -- https://master.php.net/note/delete/77693/spam
Del: non-english -- https://master.php.net/note/delete/77693/non-english
Del: in docs -- https://master.php.net/note/delete/77693/in+docs
Del: other reasons-- https://master.php.net/note/delete/77693
Reject -- https://master.php.net/note/reject/77693
Search -- https://master.php.net/manage/user-notes.php