defined

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

defined檢查某個(gè)名稱的常量是否存在

說明

defined(string $constant_name): bool

檢查該名稱的常量是否已定義。

注意:

如果你要檢查一個(gè)變量是否存在,請(qǐng)使用 isset()。 defined() 函數(shù)僅對(duì) 常量 有效。如果你要檢測(cè)某個(gè)函數(shù)是否存在,使用 function_exists()

參數(shù)

constant_name

常量的名稱。

返回值

如果名稱 constant_name 的常量已定義,返回 true;未定義則返回 false

范例

示例 #1 檢查常量

<?php
/* 注意引號(hào)的使用,這很重要。   這個(gè)例子是檢查
 * 如果字符串 'TEST' 是 TEST 常量的名稱 */
if (defined('TEST')) {
    echo 
TEST;
}
?>

參見

  • define() - 定義一個(gè)常量
  • constant() - 返回一個(gè)常量的值
  • get_defined_constants() - 返回所有常量的關(guān)聯(lián)數(shù)組,鍵是常量名,值是常量值
  • function_exists() - 如果給定的函數(shù)已經(jīng)被定義就返回 true
  • 關(guān)于 常量 的章節(jié)