define
Description
The define of Miscellaneous for PHP defines a named constant.
Syntax
define( string $constant_name, mixed $value ): 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.
WARNING: While it is possible to define resource constants, it is not recommended and may cause unpredictable behavior.
Return
Returns true on success or false on failure.
Examples
1 · constant_name value · null
<? $constant_name = "MYCONSTANT"; $value = null; define($constant_name, $value); var_export(MYCONSTANT); ?>
NULL
2 · constant_name value · boolean
<? $constant_name = "MYCONSTANT"; $value = true; define($constant_name, $value); var_export(MYCONSTANT); ?>
true
3 · constant_name value · float
<? $constant_name = "MYCONSTANT"; $value = 0.1; define($constant_name, $value); echo MYCONSTANT; ?>
0.1
4 · constant_name value · integer
<? $constant_name = "MYCONSTANT"; $value = 1; define($constant_name, $value); echo MYCONSTANT; ?>
1
5 · constant_name value · string
<? $constant_name = "MYCONSTANT"; $value = "string"; define($constant_name, $value); echo MYCONSTANT; ?>
string
6 · constant_name value · array
<? $constant_name = "MYCONSTANT"; $value = [0, 1]; define($constant_name, $value); print_r(MYCONSTANT); ?>
Array ( [0] => 0 [1] => 1 )
7 · return
<? $constant_name = "MYCONSTANT"; $value = [0, 1]; $return = define($constant_name, $value); var_export($return); ?>
true