Can you undefine or change a constant in PHP?
-
48In other words, you want a constant to be a variable? Might want to think about this for a while longer. – Marc B Jun 23 '11 at 14:41
-
6@MarcB but appropriate, given OP's username. – abligh Jan 20 '17 at 11:05
-
1@MarcB The OP was clearly acknowledging the irony of the question (he/she made an account just to post this question), but posing it seriously nonetheless. It sounds more reasonable posed like _"Can you undefine one of the things the creators of PHP decided to call 'constants'?"_ – Chris L Feb 25 '17 at 23:43
-
7In the 'C' world, this has precedence. Defines are often undefined and then redefined -- so think about that ;) – ReverseEMF Jun 01 '17 at 15:58
-
1It doesn't matter if it makes sense to undefine a constant. It's a valid question with a definite answer. – alexg Oct 25 '21 at 11:25
6 Answers
No. Constants are constant.
Reference: php.net/manual/language.constants.php

- 28,485
- 8
- 71
- 90
-
so then PHP encourages use of global variables to achieve this? – TheConstantGardener Jun 23 '11 at 14:40
-
5@TheConstantGardener: Not necessarily. There are few cases where good design requires a global variable. – George Cummins Jun 23 '11 at 14:41
-
2then why Wordpress uses the global keyword in 66 files for 851 times? – TheConstantGardener Jun 23 '11 at 14:50
-
46@TheConstantGardener: The unavoidable answer is: "Because Wordpress is poorly designed." – George Cummins Jun 23 '11 at 14:52
-
Perhaps You wanted to change constant values for some unit test cases ? – Pierre-Gilles Levallois Feb 27 '13 at 11:10
-
2In case you're wondering why someone edited your answer, it's because [it's being discussed on Meta](http://meta.stackoverflow.com/q/341743/3258851). – Marc.2377 Jan 19 '17 at 05:46
-
16Y'know George, maybe we need to include the definitions from both the dictionary *and* the PHP manual, just to be sure. You never know if the definition of the word "constant" will... wait for it... change. (I'm here all week.) – BoltClock Jan 19 '17 at 05:56
-
2It's not a constant, it's a "define". In the 'C' world (the language PHP was, loosely, modeled after), a define can be undefined and changed. Why call it a "define" if it's intended to be "constant"? In 'C', the "const" keyword creates an ACTUAL constant. A #define, is NOT really a constant (in that it can be undefined and re-defined with a different value). In 'C', any uses of that define will have a different meaning after the undefine/re-define. Thus, those of us with a coding scope broader than PHP, are likely to see the word "define" and wonder if there's an "undefine". – ReverseEMF Jun 01 '17 at 21:45
-
1@ReverseEMF Your point is well taken with regard to the C ecosystem, but in PHP they are constants in both name and practice (http://php.net/manual/en/language.constants.php). Since the OP asked the question in the PHP context, I restricted my answer to the same. – George Cummins Jun 05 '17 at 22:20
I know this is late to the game... but here is one thing that might help some people...
In my "Application.php" file (where I define all my constants and include in all my scripts) I do something like this:
if( !defined( "LOGGER_ENABLED" )){
define( "LOGGER_ENABLED", true );
}
So normally, every script is going to get logging enabled... but if in ONE particular script I don't want this behavior I can simply do this BEFORE I include my Application.php:
define( "LOGGER_ENABLED", false );

- 515
- 4
- 8
-
1Without seeing your entire app this may be an unfair assumption, but "overriding" your own system sounds a bit hacky. Can't you re-design your app/framework to better manage if Logger should be enabled or not based on something more specific or logical? As it is, this sounds very fiddly to use and manage, and over the top for something which sounds like is used infrequently. It's a valid answer adding potentially useful info for the question so not downvoting, but it potentially promotes bad practice. – James Oct 05 '14 at 21:32
-
@patrick, yes but what if someone wants to change the value which are already set, for that i think Nils Luxton answer is best suits it. Hope you remember me. – Dipesh Parmar Aug 07 '15 at 08:05
-
5This should be accepted answer as it provides a solution that works. It is also usual practice in eg. C coding, so I don't see it a problem to be used in PHP – Siniša Jan 26 '17 at 04:20
If you absolutely need to do this (although I wouldn't recommend it as others have stated) you could always use Runkit.
http://www.php.net/manual/en/function.runkit-constant-redefine.php
http://www.php.net/manual/en/function.runkit-constant-remove.php

- 766
- 3
- 14
-
30Nonsense like this is one of the reasons there is so much bad PHP code out there. Subverting the language constructs in such a manner is a terrible idea, and reflects poorly on the developer who does it. – George Cummins Jun 23 '11 at 14:46
-
3Runkit is an optional PHP extension, that I've never seen installed on any web host. – gnud Jun 23 '11 at 14:49
-
10@George Cummins I agree with you there! Still, the OP didn't mention what they were doing or why they wished to unset/redefine constants, and others may be after something like Runkit for some very experimental language-hacking stuff, so I thought I'd post it. – Nils Luxton Jun 23 '11 at 15:10
-
4I definitely see a legit purpose for this. Some applications (Joomla components) provide their translations as constants. If you want to change a translation without changing the original source code/language file you can do it all in your custom language file using the functions above - as language files and source code tend to get update every now and then. Sure, using constants for translation is surely a bad way - but you have to work with what you got, sometimes. – Deckard May 13 '14 at 09:57
-
1Maybe the programmer wants to remove leftovers from a library or an included script. – caiosm1005 Oct 15 '14 at 15:43
-
1Another use for this might be where you have default application settings that you want to override with installation application settings. Anyway, just use a static class with public properties. – OCDev Nov 18 '15 at 02:26
-
No. Once a constant is defined, it can never be changed or undefined.

- 2,001
- 13
- 28
-
4This is incorrect. The runkit extension (as mentioned in another answer) has allowed it since PHP 4. More recently the uopz extension also allows it from PHP 5.4. – Nick Rice May 11 '16 at 07:55
As not mentioned elsewhere, the uopz extension allows a constant to be deleted via uopz_undefine(), for PHP 5.4+.

- 1,163
- 1
- 13
- 21
-
4
-
1You'll need to install and enable the User Operations for Zend extension for this to work, https://www.php.net/manual/en/uopz.installation.php – Peter Kionga-Kamau Jan 08 '20 at 00:55
The other posters are correct - you can't do this. But perhaps you can move your definition to the point where you know what the best value for the constant would be.
Perhaps you're defining constants in a big list:
define('STRING1','Foo');
define('STRING2', 'Bar');
define('STRING3', 'Baz');
and you want to change the value of STRING2 once you discover a condition. One way would be to defer the definition until you know the correct setting.
define('STRING1','Foo');
// define('STRING2', 'Bar'); -- wait until initialization
define('STRING3', 'Baz');
...
if (condition) {
define('STRING2', 'Bar type 2');
} else {
define('STRING2', 'Bar type 1');
}
The logic setting STRING2 could even be in a different file, later on in your processing.

- 19,102
- 10
- 61
- 83