Page 1 of 1
Urgent question regarding global variables and include files
Posted: Wed Oct 31, 2007 12:02 am
by Josh1billion
I have three files:
include1.php
include2.php
main.php
Code: Select all
// include1.php
$global_string = "";
Code: Select all
// include2.php
$global_string .= "hello there";
Code: Select all
// main.php
include("include1.php");
$global_string .= "blah blah blah..."; // add onto it
// include include2.php, which will add onto it even more
include("include2.php");
print $global_string;
You see what I'm trying to do here-- set a variable in one include, add onto it in another include, then access it all in the main page. The output of main.php should then be "blah blah blah...hello there"
Here's the problem: include2.php doesn't know that $global_string exists!
Code: Select all
Notice: Undefined variable: global_string in include2.php
How can I make include2.php be able to use the variable?
Posted: Wed Oct 31, 2007 12:10 am
by Kieran Huggins
make it global!
Code: Select all
$_GLOBAL['string'] = "";
$_GLOBAL['string'] .= "Hello There";
Posted: Wed Oct 31, 2007 12:10 am
by s.dot
Based on the code you've given, it should be able to access the variable.
Did you post the whole code, or just enough so we'll see the point?
Posted: Wed Oct 31, 2007 12:12 am
by s.dot
Kieran Huggins wrote:make it global!
Code: Select all
$_GLOBAL['string'] = "";
$_GLOBAL['string'] .= "Hello There";
Includes inherit the variable scope of the code on which the line of the include is called. Your global suggestion suggests that the variable doesn't have sufficient scope, which it does.
I suspect there's more code!
Posted: Wed Oct 31, 2007 12:18 am
by Josh1billion
The code I posted was just example code based off of my real code.
Now I figured it out. I had to use $GLOBALS['global_string'] rather than $global_string. Kieran's post is close, and after reading his post (after I'd already figured it out), I thought maybe $_GLOBAL was better than $GLOBALS, so I changed it, and that broke it.. so I changed it back to the original $GLOBALS['global_string'] and now it all works!

**
**no idea why $GLOBALS works and $_GLOBAL doesn't, because they sound like they do the same thing.
So, to recap, replace $global_string with $GLOBALS['global_string'] in my earlier post's code and all works happily ever after.
Posted: Wed Oct 31, 2007 12:24 am
by Kieran Huggins
...don't know why I used an underscore there, it is just $GLOBALS[]
scottayy is dead on though: includes inherit the scope of the block they're included in. In any scope, $GLOBALS[] will work, but using it at all indicates poor design. maybe you should consider using an object? Is this for templating?
Posted: Wed Oct 31, 2007 12:28 am
by Josh1billion
This is for having a page render to a global variable rather than to print (and then having that global variable's contents printed out to the screen later). Long story behind all this.. but phew the results are gold.
Posted: Wed Oct 31, 2007 2:44 am
by rung
I think
scottayy was right. There's no variable scope problem here. Of course $GLOBALS works just fine, but the original code works just fine too. I tried on my machine and it said:
Is the above what you expected?
This is the 3rd time. Hope the post will be posted
