Page 1 of 1

define() - having problems to set CONSTANT value

Posted: Sat Feb 05, 2011 6:55 pm
by El Vasco
Hi All,
I am having problems with the define() statement, in some cases it seems not to have any effect on the Constant I am trying to declare and the Constant keeps the value that was given to it in previous Functions.

If in a previous Function I declared:
define('DB_TABLE', 'old_table');

then running another Function in which I declare:
define('DB_TABLE', 'new_table');
echo DB_TABLE; // displays 'old_table' Previously defined in another Function not taking the new value...

Does anybody has any clues why this is happening ?

Thanks !
El Vasco

Re: define() - having problems to set CONSTANT value

Posted: Sat Feb 05, 2011 8:20 pm
by califdon
The scope of a defined constant is global and cannot be redefined. You can't define the same variable in different functions. Whichever function is called first defines the constant. That's the difference between a variable and a constant.

Re: define() - having problems to set CONSTANT value

Posted: Sat Feb 05, 2011 8:51 pm
by s.dot
Might be better to use a config array or an ini file

Re: define() - having problems to set CONSTANT value

Posted: Sun Feb 06, 2011 10:17 am
by El Vasco
Thank You, Both ! That answers my question and solves my problem :-)
Tx !
El Vasco