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

定数の値。

警告

リソース型の定数を定義することもできますが、 推奨できません。予期せぬ振る舞いをする可能性があります。

case_insensitive

true を指定すると、定数は大文字小文字を区別しないようになります。 デフォルトでは大文字小文字を区別します。つまり CONSTANTConstant は別の値を表すわけです。

警告

大文字小文字を区別しない定数を定義するのは、 PHP 7.3.0 以降では推奨されなくなりました。 PHP 8.0.0 以降では、false のみを受け入れます。 true を渡すと、警告が発生します。

注意:

大文字小文字を区別しないときは、定数は小文字で格納されます。

戻り値

成功した場合に true を、失敗した場合に false を返します。

変更履歴

バージョン 説明
8.1.0 value に、オブジェクトを渡せるようになりました。
8.0.0 case_insensitivetrue を渡すと、 E_WARNING が発生するようになりました。 false を渡すことはまだ許可されています。
7.3.0 case_insensitive は非推奨になりました。 8.0.0 で削除される予定です。

例1 定数の定義

<?php
define
("CONSTANT", "Hello world.");
echo
CONSTANT; // "Hello world." を出力します
echo Constant; // "Constant" を出力し、警告が発生します

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)

参考

  • defined() - 指定した名前の定数が存在するかどうかを調べる
  • constant() - 定数の値を返す
  • 定数の節

add a note

User Contributed Notes 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