PHP define()Function

PHP Misc 参考手册PHP Misc Reference Manual

Examples

Define a case-sensitive constant:

<?php
define
("GREETING","Hello you! How are you today?");
echo
constant("GREETING");
?>

Run Example »

Definition and Usage

The define() function defines a constant.

Constants are similar to variables, except that:

  • After being set, the value of a constant cannot be changed
  • Constant names do not need a leading dollar sign ($)
  • Scope does not affect access to constants
  • Constant values can only be strings and numbers

Syntax

define(name,value,case_insensitive)

Parameter Description
name Required. Specifies the name of the constant.
value Required. Specifies the value of the constant. PHP 7 supports arrays, as in the example below:
<?php
// PHP7+ 支持
define('ANIMALS', [
    'dog',
    'cat',
    'bird'
]);

echo ANIMALS[1]; // 输出 "cat"
?>
case_insensitive Optional. Specifies whether the constant name is case-sensitive. Possible values:
  • TRUE - Case-insensitive
  • FALSE - Default. Case-sensitive

Technical Details

Return value: Returns TRUE on success, or FALSE on failure.
PHP Version: 4+


More Examples

Example

Define a case-insensitive constant:

<?php
define
("GREETING","Hello you! How are you today?",TRUE);
echo
constant("greeting");
?>

Run Example »


PHP Misc 参考手册PHP Misc Reference Manual Other Extensions