Urgent question regarding global variables and include files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Urgent question regarding global variables and include files

Post 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?
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

make it global!

Code: Select all

$_GLOBAL['string'] = ""; 

$_GLOBAL['string'] .= "Hello There";
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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!
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

:oops:

...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?
User avatar
Josh1billion
Forum Contributor
Posts: 316
Joined: Tue Sep 11, 2007 3:25 pm

Post 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.
rung
Forum Newbie
Posts: 3
Joined: Tue Oct 30, 2007 10:04 pm

Post 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:

Code: Select all

blah blah blah...hello there
Is the above what you expected?


This is the 3rd time. Hope the post will be posted :(
Post Reply