Posted by Wescotte on 02/22/06 20:27
I'm having an issue where what should be global variables are not in
scope and I'm confused as to why.
In Case 1 the variables are not in scope for functions in File2
In Case 2 the varibales are in scope for access in File1's functions
AFTER the include
In Case 3 the varibales are in scope for functionsin File2
Why does case 1 not work but case 3 does? Does case 2 the proper method
to use?
CASE 1:
File1:
switch (OBJ) {
case 1:
include "file2.php"
if Function_In_File2()
....
break;
}
File2:
<?php
$MY_GLOBAL_VARIABLE = "TEST";
Function Function_In_FIle()
{
global $MY_GLOBAL_VARIABLE;
echo $My_GLOBAL_VARIABLE; /* Produces Notice: Undefined variable:
$My_GLOBAL_VARIABLE in File2.php on line xx */
}
?>
CASE 2:
File1:
switch (OBJ) {
case 1:
include "file2.php"
if Function_In_File2()
....
break;
}
File2:
<?php
global $MY_GLOBAL_VARIABLE; $MY_GLOBAL_VARIABLE = "TEST";
Function Function_In_FIle()
{
global $MY_GLOBAL_VARIABLE;
echo $My_GLOBAL_VARIABLE; /* Displays "TEST" */
}
?>
CASE 3:
File1:
switch (OBJ) {
case 1:
include "file2.php"
echo $MY_GLOBAL_VARAIBLE; /* Displays "TEST" Why does this work and
not case 1? */
}
File2:
<?php
$MY_GLOBAL_VARIABLE = "TEST";
?>
Navigation:
[Reply to this message]
|