Can we redefine a constant in PHP

I have defined a constant as

define(‘EMAIL_USE_HTML’, ‘false’);

on one of my include file.

but i need to redefine the constant EMAIL_USE_HTML on another page to true.

like … define(‘EMAIL_USE_HTML’, ‘true’);

when calling the EMAIL_USE_HTML value on newpage, it returns the old value as false.

As checked from php.net, i found on a post that constants can not be redeclared.

Can i redeclare or any alternate tech.

Thanks

Well a constant means it doesn’t change. So if you need it to change, why not just use a variable instead?

Requirement itself seems odd and not in a good logic. If you want to change the value then that is never called a constant. So use some variable instead of constant. Isn’t it very simple? Or let us know your particular requirement to do so…

Well, this is the phrase which clears my doubt why we can not redeclare a defined variable in php.

Any how solving the issue with variables is not a problem for me. what i was expecting about the logic of reassigning value to a defined constant.

Thanks to all of you!

That’s a rule against the purpose of the constants in PHP. That’s it! You can check whether a constant is defined already or not with the defined() function. If it is not defined then you can define it.

A constant’s value, once set, remains for the iteration of that script. If you need it to have a different value on a different iteration that will work. It’s not even that unusual.

An example of this is at the very first line of my own scripts. define(‘STARTED’, microtime()); Obviously this “constant” will have different value each time the script runs, but for any given iteration of the script it will stay the same (and it’s main purpose is to time script execution for script debugging).

Constants do not persist from the execution of the scripts of one page to the next. For that sort of thing you’ll need to pass parameters to a form, or cookies to the browser, or perhaps most commonly a $_SESSION variable. Each approach has advantages and disadvantages.

The most common (and in fact the only) way to pass constants from one page to another is to place them in an include file and include or require that from each page. If you try to pass it any other way then it is no longer a constant.

:nono: The OP wasn’t asking how to define a constant for his entire project. He was asking if he could define a constant as false on one page then as true on the next. He got a bunch of wise guys telling him that once a constant was defined it couldn’t be redefined leaving the original question incorrectly answered. The fact being that you can defined a constant different ways on different iterations of a script (whether that should be done is open for debate).

He didn’t ask whether he could use an include file to make his constant declarations permanent. I would think the answer to that is rather obvious and providing an answer to it without being asked is a snide insult to the OP and me felgall. I wish you’d knock that sort of thing off.

Actually you can, but only if you have runkit installed. See: http://php.net/runkit_constant_remove

The runkit extension provides means to modify constants, user-defined functions, and user-defined classes. It also provides for custom superglobal variables and embeddable sub-interpreters via sandboxing.

This package is meant as a feature added replacement for the » classkit package. When compiled with the --enable-runkit=classkit option to ./configure, it will export classkit compatible function definitions and constants.

If you need something that you can set and change, you should look into class variables or [URL=“http://php.net/manual/en/reserved.variables.globals.php”]globals.

Talk about insults.

The OP asked if a constant could be changed. If I wanted to be a “wise guy” I would have said something in the lines of “What is the definition of a constant?”. I answered his question and gave him a valid solution.

I tend to agree with you. felgall answered a question that had not been asked in the first place. That is the sort of things he does sometimes. I noticed.:slight_smile:

I was responding to Michael Morris’ post about passing constants between pages - pointing out that the methods being suggested were for passing variables between pages and not for passing constants between pages.
chael’s answer was misleading in that the values passed that way are no longer constants.

While it may make perfect sense for the OP in that they actually don’t want to pass constants there may be a lot of otople who find this thread looking for an answer as to how to pass constants between pages since that was what Micheael implied his answer was explaining and while his response answered the OP’s question it would not be the correct answer for those who found his post while searching the forum for how to pass constants between pages.

Answers in forums are not just going to be used by the OP. They will also be used by anyone else who finds them and thinks that they apply to their situation.

Agreed! Though the problem is only OP’s for now but this can be used by thousand of people who search in the forum (or even search engine) who run in the same problem.

Once again, if the value is changed then that is never called as a constant anyway. And the value of a constant cannot be changed in anyway, and that is a rule in PHP.

I think i must clear this point for future readers.

Yes i was using a common include file on each page, which i did mention on my original post also
( due to which the defined CONSTANT is unchanged on all pages where the include file is called.)