Forum Moderators: coopster
I'm not too sure how to explain this, but i'll give it a shot
Basically I have one file with this function:
function foo() {
include("file.php");
}
and in file.php:
$var = "text";
function bar() {
global $var;
echo $var;
}
I've tried using $_GLOBALS as well as globaling $var in foo function but can't seem to get the value of $val in bar
Any ideas? Am I just being stupid? :/
-------index.php-------
<?
function foo(){
include 'filez.php';
}
foo();
echo $var; //does not work
echo $str; //does not work
bar($var); //does not work
?>
-----filez.php------
<?
$var = "Hello from bar()";
function bar($str){
echo $str;
}
bar($var);
?>
$var is not a global variable using the design you have here, it is still "inside" a function, not defined in the global namespace yet. It is easier to see if you actually move the included file into the local script ...
function foo() {
// include("file.php"); (inserted below)
$var = "text"; // <-- not a global!
function bar() {
global $var;
echo $var;
}
} $var = 'I am a global variable!';
function foo() {
//include("file.php");
$var = "text"; // <-- not a global!
function bar() {
global $var;
echo $var;
}
bar();
}
foo();
I am a global variable!