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
- Constant names do not need to start with a dollar sign ($)
- Scope does not affect access to constants
- 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 name of the constant 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!