Be aware that if "Notice"-level error reporting is turned off, then trying to use a constant as a variable will result in it being interpreted as a string, if it has not been defined.
I was working on a program which included a config file which contained:
<?php
define('ENABLE_UPLOADS', true);
?>
Since I wanted to remove the ability for uploads, I changed the file to read:
<?php
//define('ENABLE_UPLOADS', true);
?>
However, to my surprise, the program was still allowing uploads. Digging deeper into the code, I discovered this:
<?php
if ( ENABLE_UPLOADS ):
?>
Since 'ENABLE_UPLOADS' was not defined as a constant, PHP was interpreting its use as a string constant, which of course evaluates as True.define
(PHP 4, PHP 5, PHP 7, PHP 8)
define — Defines a named constant
Description
Defines a named constant at runtime.
Parameters
constant_name-
The name of the constant.
Note:
It is possible to define() constants with reserved or even invalid names, whose value can (only) be retrieved with constant(). However, doing so is not recommended.
value-
The value of the constant.
WarningWhile it is possible to define resource constants, it is not recommended and may cause unpredictable behavior.
case_insensitive-
If set to
true, the constant will be defined case-insensitive. The default behavior is case-sensitive; i.e.CONSTANTandConstantrepresent different values.WarningDefining case-insensitive constants is deprecated as of PHP 7.3.0. As of PHP 8.0.0, only
falseis an acceptable value, passingtruewill produce a warning.Note:
Case-insensitive constants are stored as lower-case.
Changelog
| Version | Description |
|---|---|
| 8.1.0 |
value can now be an object.
|
| 8.0.0 |
Passing true to case_insensitive now emits an E_WARNING. Passing false is still allowed.
|
| 7.3.0 |
case_insensitive has been deprecated and will be removed in version 8.0.0.
|
Examples
Example #1 Defining Constants
<?php
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
echo Constant; // outputs "Constant" and issues a notice.
define("GREETING", "Hello you.", true);
echo GREETING; // outputs "Hello you."
echo Greeting; // outputs "Hello you."
// Works as of PHP 7
define('ANIMALS', array(
'dog',
'cat',
'bird'
));
echo ANIMALS[1]; // outputs "cat"
?>
Example #2 Constants with Reserved Names
This example illustrates the possibility to define a constant with the same name as a magic constant. Since the resulting behavior is obviously confusing, it is not recommended to do this in practise, though.
<?php
var_dump(defined('__LINE__'));
var_dump(define('__LINE__', 'test'));
var_dump(constant('__LINE__'));
var_dump(__LINE__);
?>The above example will output:
bool(false) bool(true) string(4) "test" int(5)
See Also
- defined() - Checks whether a constant with the given name exists
- constant() - Returns the value of a constant
- The section on Constants
User Contributed Notes 4 notes
Not sure why the docs omit this, but when attempting to define() a constant that has already been defined, it will fail, trigger an E_NOTICE and the constant's value will remain as it was originally defined (with the new value ignored).
(Guess that's why they're called "constants".)define() will define constants exactly as specified. So, if you want to define a constant in a namespace, you will need to specify the namespace in your call to define(), even if you're calling define() from within a namespace. The following examples will make it clear.
The following code will define the constant "MESSAGE" in the global namespace (i.e. "\MESSAGE").
<?php
namespace test;
define('MESSAGE', 'Hello world!');
?>
The following code will define two constants in the "test" namespace.
<?php
namespace test;
define('test\HELLO', 'Hello world!');
define(__NAMESPACE__ . '\GOODBYE', 'Goodbye cruel world!');
?>Found something interesting. The following define:
<?php
define("THIS-IS-A-TEST","This is a test");
echo THIS-IS-A-TEST;
?>
Will return a '0'.
Whereas this:
<?php
define("THIS_IS_A_TEST","This is a test");
echo THIS_IS_A_TEST;
?>
Will return 'This is a test'.
This may be common knowledge but I only found out a few minutes ago.
[EDIT BY danbrown AT php DOT net: The original poster is referring to the hyphens versus underscores. Hyphens do not work in defines or variables, which is expected behavior.]