note 21758 deleted from function.include by sniper
| From: | sniper@php.net | Date: | Wed, 29 Jan 2003 04:10:47 +0000 |
| Subject: | note 21758 deleted from function.include by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-42984@lists.php.net to get a copy of this message | ||
I noticed that include() doesn't affect variable scope. Any variables defined outside the
included file are available in it and, most importantly, any variables defined in the include file
don't go out of scope at the end of it.
I run into some trouble when reusing variable names inside the include and after the point where it
was included in the parent file, e.g.:
<?php
// This is the included file
$myvar = "Something else";
?>
<?php
// This is the parent file
$myvar = "something";
include("included_file");
echo $myvar;
?>
results in "Something else" being output!
My solution was to enclose all the code in the included file in a function
declaration and call that function at the end of the file, like this:
<?php
// This is my new included file
function included_file()
{
$myvar = "Something else";
}
included_file();
?>