constant
(PHP 4 >= 4.0.4, PHP 5, PHP 7)
constant — 定数の値を返す
説明
constant
( string $name
) : mixed
name
で指定した定数の値を返します。
constant() はある定数の値を取得する必要があるが、 その名前が不明な場合に有用です。これは、定数名が変数に保存されているか、 関数により返されるかの場合です。
この関数は クラス定数に対しても動作します。
パラメータ
name
- 定数名。
返り値
定数の値、あるいはその定数が定義されていない場合に null
を返します。
エラー / 例外
定数が定義されていない場合は E_WARNING
レベルのエラーが発生します。
例
例1 constant() の例
<?phpdefine("MAXSIZE", 100);echo MAXSIZE;echo constant("MAXSIZE"); // ひとつ前の行と同じことですinterface bar { const test = 'foobar!';}class foo { const test = 'foobar!';}$const = 'test';var_dump(constant('bar::'. $const)); // string(7) "foobar!"var_dump(constant('foo::'. $const)); // string(7) "foobar!"?>