Forum Moderators: coopster

Message Too Old, No Replies

Weird scope issue from $GLOBALS

         

jatar_k

3:08 pm on Nov 19, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



So I have a function that can't seem to test a global variable

the variable is defined in a lib like so
$MY_COOL_VAR = true;

then it is tested as such inside of a function
if($MY_COOL_VAR) echo 'watch me do something';

so both of these files are included by a 3rd scipt

during execution I can dump $GLOBALS and the variable is there with the correct value.

anyone have any thoughts on why it isn't imported into the variable table in the included script?

I can't say I've seen this, still plodding through code to look for anything odd

enigma1

3:22 pm on Nov 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if you're accessing it inside a function you need to declare it as global within the function scope ie:

function test() {
global $MY_COOL_VAR;

echo $MY_COOL_VAR;
.....
}

eelixduppy

3:23 pm on Nov 19, 2008 (gmt 0)



Within user-defined functions the variable defined in the other file doesn't exist. The vars used within the functions have local scope. You must use the global keyword to specify that it should find it in the globals. This in fact, should work:

global $MY_COOL_VAR;
if($MY_COOL_VAR) echo 'watch me do something';

[edit]
too slow :)

coopster

3:26 pm on Nov 19, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Or just use the $GLOBALS superglobal which has scope everywhere, including inside a function.
if (isset($GLOBALS['MY_COOL_VAR']) && $GLOBALS['MY_COOL_VAR']) {

jatar_k

3:35 pm on Nov 19, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that I've done

I think it's because the joker before me included everything in every function, which I removed, everywhere

got me messed

so his whole application is based on a scope mismatch, how absolutely wonderful

makes my whole day

funny how playing with other people's junk gets you all messed up so easily

eelixduppy

3:37 pm on Nov 19, 2008 (gmt 0)



Sounds like a mess. To not go completely insane you should avoid spaghetti for dinner tonight. ;)

jatar_k

3:41 pm on Nov 19, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hehe

so is anyone else jealous of how many responses I got so quickly? don't be, that's how badly people like to show me up, hehe

simple software is sometimes the worst, so you repeat an error 100 times, no biggie, but when you repeat 5-10 errors in every function, it becomes a bit of a burden

at least it keeps me busy ;)

coopster

3:44 pm on Nov 19, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



so is anyone else jealous of how many responses I got so quickly

Yes. But you asked a scope question which you yourself have answered here so many times yourself that it was obvious to me that you are so deep in analysis that you were overlooking the obvious. I hate seeing a friend struggle :)

jatar_k

3:54 pm on Nov 19, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



he's laughing on the inside

frankly I'm sitting here trying to figure out how I am going to get around this all encompassing blunder

I think i'll switch them all to constants, means I just have to remove a metric tonne of $