note 60243 added to language.variables.scope
| From: | windandwaves at osu1 dot php dot net | Date: | Sat, 31 Dec 2005 03:32:46 +0000 |
| Subject: | note 60243 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-101025@lists.php.net to get a copy of this message | ||
Consider the files and their content below:
--index.php
<?php
include_once("inc2.php");
include_once("inc1.php");
goer();
echo "var 1: ".$dd_myvar1.'<br>';
echo "var 2: ".$dd_myvar2.'<br>';
echo "var 3: ".$dd_myvar3.'<br>';
?>
--inc1.php:
<?php
$dd_myvar1 = "inc1";
?>
--inc2.php:
<?php
$dd_myvar2 = "inc2";
function goer() {
include_once("inc3.php");
}
?>
--inc3.php:
<?php
$dd_myvar3 = "inc3";
function roer() {
die("ran roer");
}
?>
opening index.php in your browser will produce:
var 1: inc1
var 2: inc2
var 3:
It took me a while to work out why var 3 was not showing. From this simplified example it is
obvious that because the include statement in inc2.php is in a function, it will only apply to that
function (taking the include function out of the function will show var3).
Now, if we add
roer();
as the last line to index.php and then open it in the browser then you will see:
inc var 1: inc1
inc var 2: inc2
inc var 3:
ran roer
This may be confusing. While the variables loaded from goer could not be seen, the function loaded
was available in index.php.
----
Manual Page -- http://www.php.net/manual/en/language.variables.scope.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60243
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60243&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60243&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60243&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60243&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60243&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60243&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60243&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60243&report=yes
Search -- http://master.php.net/manage/user-notes.php