update page now
PHP 8.5.2 Released!

define

(PHP 4, PHP 5, PHP 7, PHP 8)

define定义一个常量

说明

define(string $constant_name, mixed $value, bool $case_insensitive = false): bool

在运行时定义一个常量。

参数

constant_name

常量名。

注意:

可以用 define() 定义保留关键词甚至无效名称的常量,它的值可以(仅可以)通过 constant() 获取。 不过,不推荐这么做。

value

常量的值。

警告

常量还可以定义为 resource 类型,但并不推荐这样做,因为可能会有不可预知的行为发生。

case_insensitive

如果设置为 true,则该常量不区分大小写。默认是区分大小写的。比如, CONSTANTConstant 代表了不同的值。

警告

PHP 7.3.0 起,废弃定义不区分大小写的常量。自 PHP 8.0.0 开始, 只接受 false 值,传递 true 将产生警告。

注意:

不区分大小写的常量以小写的形式储存。

返回值

成功时返回 true, 或者在失败时返回 false

更新日志

版本 说明
8.1.0 value 现在可以是一个对象。
8.0.0 现在传递 truecase_insensitive 会发出 E_WARNING。仍然允许传递 false
7.3.0 废弃了 case_insensitive,并将在 8.0.0 版中移除。

示例

示例 #1 定义常量

<?php
define
("CONSTANT", "Hello world.");
echo
CONSTANT; // 输出 "Hello world."
echo Constant; // 输出 "Constant" 并导致 Notice

define("GREETING", "Hello you.", true);
echo
GREETING; // 输出 "Hello you."
echo Greeting; // 输出 "Hello you."

// PHP 7 起就可以运行了
define('ANIMALS', array(
'dog',
'cat',
'bird'
));
echo
ANIMALS[1]; // 输出 "cat"

?>

示例 #2 以保留名称定义常量

本例子说明了以 魔术常量相同名称定义常量的能力。由于行为结果过于令人迷惑,所以实践中不推荐。

<?php
var_dump
(defined('__LINE__'));
var_dump(define('__LINE__', 'test'));
var_dump(constant('__LINE__'));
var_dump(__LINE__);
?>

以上示例会输出:

bool(false)
bool(true)
string(4) "test"
int(5)

参见

添加备注

用户贡献的备注 4 notes

up
100
ravenswd at gmail dot com
10 years ago
Be aware that if "Notice"-level error reporting is turned off, then trying to use a constant as a variable will result in it being interpreted as a string, if it has not been defined.

I was working on a program which included a config file which contained:

<?php
define('ENABLE_UPLOADS', true);
?>

Since I wanted to remove the ability for uploads, I changed the file to read:

<?php
//define('ENABLE_UPLOADS', true);
?>

However, to my surprise, the program was still allowing uploads. Digging deeper into the code, I discovered this:

<?php
if ( ENABLE_UPLOADS ):
?>

Since 'ENABLE_UPLOADS' was not defined as a constant, PHP was interpreting its use as a string constant, which of course evaluates as True.
up
29
@SimoEast on Twitter
8 years ago
Not sure why the docs omit this, but when attempting to define() a constant that has already been defined, it will fail, trigger an E_NOTICE and the constant's value will remain as it was originally defined (with the new value ignored).

(Guess that's why they're called "constants".)
up
29
danbettles at yahoo dot co dot uk
16 years ago
define() will define constants exactly as specified.  So, if you want to define a constant in a namespace, you will need to specify the namespace in your call to define(), even if you're calling define() from within a namespace.  The following examples will make it clear.

The following code will define the constant "MESSAGE" in the global namespace (i.e. "\MESSAGE").

<?php
namespace test;
define('MESSAGE', 'Hello world!');
?>

The following code will define two constants in the "test" namespace.

<?php
namespace test;
define('test\HELLO', 'Hello world!');
define(__NAMESPACE__ . '\GOODBYE', 'Goodbye cruel world!');
?>
up
4
eparkerii at carolina dot rr dot com
17 years ago
Found something interesting.  The following define:

<?php
define("THIS-IS-A-TEST","This is a test");
echo THIS-IS-A-TEST;
?>

Will return a '0'.

Whereas this:

<?php
define("THIS_IS_A_TEST","This is a test");
echo THIS_IS_A_TEST;
?>

Will return 'This is a test'.

This may be common knowledge but I only found out a few minutes ago.

[EDIT BY danbrown AT php DOT net: The original poster is referring to the hyphens versus underscores.  Hyphens do not work in defines or variables, which is expected behavior.]
To Top