define()
function defines a constant.
Constants are similar to variables, the difference is:
Define a case-sensitive constant:
<?php define ( "GREETING" , "Hello world!" ) ; echo constant ( "GREETING" ) ; ?>
Try it yourself
Define a case-insensitive constant:
<?php define ( "GREETING" , "Hello world!" , TRUE ) ; echo constant ( "greeting" ) ; ?>
Output:
Hello world!
define ( name , value , case_insensitive )
parameter | describe |
---|---|
name | Required. Specifies the name of the constant. |
value | Required. Specifies the value of the constant. |
case_insensitive |
Optional. Specifies whether the constant name is case-insensitive. Possible values:
|
After PHP 7.3, it is not recommended to use case-insensitive constant names because this option has been deprecated. When defining constants, a case-sensitive name should always be used.
Additionally, while PHP 7 allows arrays to be used as values for constants, this is not a typical use of constants, as constants are often used to store simple invariant values such as configuration settings or constant expressions.
In PHP 5, the value of a constant must be a scalar type (such as a string, integer, floating point number) or a Boolean value, or NULL.