[PHP 4, PHP 5, PHP 7, PHP 8]
define — Defines a named constant
Description
define[string $constant_name
, mixed $value
, bool
$case_insensitive
= false
]: bool
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. In PHP 5, value
must be a scalar value [int, float, string, bool, or null
]. In PHP 7, array values are also accepted.
Warning
While 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. CONSTANT
and Constant
represent different values.
Warning
Defining case-insensitive constants is deprecated as of PHP 7.3.0. As of PHP 8.0.0, only false
is an
acceptable value, passing true
will produce a warning.
Note:
Case-insensitive constants are stored as lower-case.
Return Values
Returns true
on success or false
on failure.
Changelog
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.
|
7.0.0 | array values are allowed. |
Examples
Example #1 Defining Constants
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.
The above example will output:
bool[false] bool[true] string[4] "test" int[5]
See Also
- defined[] - Checks whether a given named constant exists
- constant[] - Returns the value of a constant
- The section on Constants
ravenswd at gmail dot com ¶
7 years ago
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:
Since I wanted to remove the ability for uploads, I changed the file to read:
However, to my surprise, the program was still allowing uploads. Digging deeper into the code, I discovered this:
Since 'ENABLE_UPLOADS' was not defined as a constant, PHP was interpreting its use as a string constant, which of course evaluates as True.
@SimoEast on Twitter ¶
5 years ago
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".]
danbettles at yahoo dot co dot uk ¶
13 years ago
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"].
The following code will define two constants in the "test" namespace.
Dale Landry ¶
5 years ago
With php 7 you can now define arrays.
consider the following code:
eparkerii at carolina dot rr dot com ¶
13 years ago
Found something interesting. The following define:
Will return a '0'.
Whereas this:
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.]
Jamie ¶
11 years ago
This is obvious, but easy to forget: if you include a file, the include file can only make use of constants already defined. For example:
chris at peeto dot net ¶
12 years ago
The value of a constant can be the value of another constant.
me at mariusgerum dot de ¶
3 years ago
You can define constants with variable names [works also with constant values or variables or array values or class properties and so on - as long it's a valid constant name].
Needless to say that you'll lose your IDE support for refactoring and highlighting completely for such cases.
No clue why someone would / could actually use this but i thought it's worth mentioning.
swisschocolate at cmail dot nu ¶
6 years ago
I think worth mentioning is that define[] appears to ignore invalid constant names.
One immediate implication of this seem to be that if you use an invalid constant name you have to use constant[] to access it and obviously that you can't use the return value from define[] to tell you whether the constant name used is invalid or not.
For example:
$name = '7[/!§%';
var_dump[define[$name, "hello"]]; // outputs bool[true]
var_dump[constant[$name]]; // outputs string[5] "hello"
axew3 at axew3 dot com ¶
6 years ago
Php 7 - Define: "Defines a named constant at runtime. In PHP 7, array values are also accepted."
But prior PHP 7, you can maybe do this, to pass an array elsewhere using define:
$to_define_array = serialize[$array];
define[ "DEFINEANARRAY", $to_define_array ];
... and so ...
$serialized = DEFINEANARRAY; // passing directly the defined will not work
$our_array = unserialize[$serialized];
print_r[$our_array];
Anonymous ¶
5 years ago
There's an undocumented side-effect of setting the third parameter to true [case-insensitive constants]: these constants can actually be "redefined" as case-sensitive, unless it's all lowercase [which you shouldn't define anyway].
The fact is that case-sensitive constants are stored as is, while case-insensitive constants are stored in lowercase, internally. You're still allowed to define other constants with the same name but capitalized differently [except for all lowercase].
A third party plugin might attempt to define a constant for which you already set a value. If it's fine for them to set the new value, assuming you cannot edit the plugin, you could define your constant case-insensitive. You can still access the original value, if needed, by using any capitalization other than the one the plugin uses. As a matter of fact, I can't think of another case where you would want a case-insensitive constant...
Why would you use this?
phen at adnerdum dot org ¶
11 years ago
To clear up a few thing:
Integers with 0 in front work. But since PHP [and many other languages] handle them as octal values, they're only allowed a range of 0-7:
--------------------------------------- Furthermore, writing the constant name without the quotation-marks [as mentioned in the notes] throws an E_NOTICE and should be avoided!
Result:
448
0
Result:
Notice: Use of undefined constant TEST - assumed 'TEST'
jan at webfontein dot nl ¶
14 years ago
For translating with variables and define, take also a look on the constant[] function.
Example :
David Spector ¶
3 years ago
It may be worth stating that a define function must be executed before its global constant is referenced.
Example:
Abc[];
define["TEST", 23];
function Abc[]
{
echo TEST;
} // Abc
This code fails with a Notice-level message. TEST is treated here as being the string "TEST".
agneady at gmail dot nospam dot please dot com ¶
11 months ago
If you happen to name your constant the same as a function name [either a built-in function or a user-defined one], PHP can handle this correctly based on context. For example:
Output:
MYFUNC[]: function output, MYFUNC: constant value
ohcc at 163 dot com ¶
2 years ago
A namespace constant can be defined using the define function, the constant defined this way is not global.