PHP define() Function
Definition and Usage
The define() function defines a constant.
Constants are similar to variables, the difference is:
- After setting, the value of the constant cannot be changed
- The constant name does not need a dollar sign ($) at the beginning
- Scope does not affect access to the constant
- The constant value can only be a string or a number
Syntax
define(name,value,case_insensitive)
Parameters | Description |
---|---|
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-sensitive. If set to true, it is case-insensitive. The default is false (case-sensitive). |
Instance
Example 1
Define a case-sensitive constant:
<?php define("GREETING","Hello world!"); echo constant("GREETING"); ?>
Output:
Hello world!
Example 2
Define a case-insensitive constant:
<?php define("GREETING","Hello world!",TRUE); echo constant("greeting"); ?>
Output:
Hello world!