define() - having problems to set CONSTANT value

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
El Vasco
Forum Newbie
Posts: 15
Joined: Tue Jan 04, 2011 12:05 pm

define() - having problems to set CONSTANT value

Post 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
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

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

Post by s.dot »

Might be better to use a config array or an ini file
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.
El Vasco
Forum Newbie
Posts: 15
Joined: Tue Jan 04, 2011 12:05 pm

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

Post by El Vasco »

Thank You, Both ! That answers my question and solves my problem :-)
Tx !
El Vasco
Post Reply