Menu Home

define

osbo.com | PHP | Functions | define

Defines a named constant

Syntax

define ( string $name , mixed $value ) : bool

Parameters

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 (integer, float, string, boolean, 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.

Return

Returns TRUE on success or FALSE on failure.

Examples

name value | null

<?

define("MYCONSTANT", null);

var_export(MYCONSTANT);

?>
NULL

name value | boolean

<?

define("MYCONSTANT", true);

var_export(MYCONSTANT);

?>
true

name value | float

<?

define("MYCONSTANT", 0.1);

echo MYCONSTANT;

?>
0.1

name value | integer

<?

define("MYCONSTANT", 1);

echo MYCONSTANT;

?>
1

name value | string

<?

define("MYCONSTANT", "Hello World!");

echo MYCONSTANT;

?>
Hello World!

name value | array

<?

define("MYCONSTANT", array(0, 1));

print_r(MYCONSTANT);

?>
Array
(
    [0] => 0
    [1] => 1
)

Return

<?

$return = define("MYCONSTANT", array(0, 1));

var_export($return);

?>
true

PHP