constant

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)

constant返回一個(gè)常量的值

說(shuō)明

constant(string $name): mixed

返回 name 對(duì)應(yīng)的常量的值。

當(dāng)你不知道常量名,卻需要獲取常量的值時(shí),constant() 就很有用了。也就是說(shuō),常量名儲(chǔ)存在一個(gè)變量里,或者由函數(shù)返回時(shí)。

該函數(shù)也適用 類(lèi)常量。

參數(shù)

name

常量名。

返回值

返回常量的值。如果常量未定義則返回 null 。

錯(cuò)誤/異常

如果常量未定義,會(huì)產(chǎn)生一個(gè) E_WARNING 級(jí)別的錯(cuò)誤。

范例

示例 #1 constant() 的例子

<?php

define
("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!"

?>

參見(jiàn)

  • define() - 定義一個(gè)常量
  • defined() - 檢查某個(gè)名稱的常量是否存在
  • 關(guān)于 常量 的章節(jié)