Possible Duplicate:
How to use include within a function?
i have two file
inc.php file
<?php
$var1 = "foo";
$var1 .= "bar";
?>
test.php File
<?php
function getcontent($file) {
include ($file);
}
getcontent('inc.php');
echo $var1;
?>
When i run test.php it give me error in output
Notice: Undefined variable: var1 in \www\test.php on line 7
But when i change my test.php file to this
<?php
include ('inc.php');
echo $var1;
?>
its works, and give me output fine
foobar
When you do it by this
it includes as
actually your variables are being included inside the function and not visible outside of the function so the error message occurs.
See this SO answer.