Forum Moderators: coopster

Message Too Old, No Replies

Global defined variables

Within functions

         

ahmedtheking

4:08 pm on May 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's know that you can access global vars simply by adding 'global $var' within your function. But, what about a constant or defined var? How can you access that without having to pass it to a local function var?

phparion

5:28 pm on May 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think constants also work as global variable because php determines at the last moment how to deal with the variable based on its value format.

I didn't get 'defined var' part...

ahmedtheking

6:12 pm on May 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well you define a var like so: define(SOMEVAR,"#*$!xx");

Now, when you take this into a function:

function test () {
global SOMEVAR;
....
}

It reports an error that it's expecting a $ sign.

phparion

7:00 pm on May 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



did you try using it directly without defining as global?

dreamcatcher

7:00 pm on May 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ahmedtheking,

As phparion mentioned, you don`t need to declare constants as they are already global in scope. So, just use the constant with no declaration.

function test () {
echo SOMEVAR;
....
}

dc

[edit]Thanks p, you beat me to it.

ahmedtheking

10:40 pm on May 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah that's fantastic! I didn't know that! Thanks guys!