constant
(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)
constant — 返回一個常量的值
參數
-
name
-
常量名。
返回值
返回常量的值。
錯誤/異常
如果常量未定義,會拋出 Error 異常。
在 PHP 8.0.0 之前,會產生一個 E_WARNING
級別的錯誤。
更新日誌
版本 | 說明 |
---|---|
8.0.0 |
如果常量未定義,constant() 現在會拋出
Error 異常。以前會產生一個
E_WARNING 級別的錯誤並返回 null 。
|
範例
示例 #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!"
?>
參見
- define() - 定義一個常量
- defined() - 檢查某個名稱的常量是否存在
- get_defined_constants() - 返回所有常量的關聯陣列,鍵是常量名,值是常量值
- 關於 常量 的章節