defined()是PHP中的內(nèi)置函數(shù),用于檢查是否存在常量,即是否定義了常量;語法格式“defined(name)”,參數(shù)name是要檢查的常量的名稱。如果常量存在,則返回TRUE,否則返回FALSE。
本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦
php defined()函數(shù)
defined() 函數(shù)檢查常量是否存在。
語法
defined(name)
name:規(guī)定要檢查的常量的名稱,不可省略。
返回值:如果常量存在,則返回 TRUE,否則返回 FALSE。
注意:此功能可用于PHP 4.0.0和更高版本。
示例1:
<?php define("constant_key", "value for the constant key"); echo defined("constant_key"); ?>
輸出:
示例2:定義常數(shù)后檢查條件是否成立。
<?php define("constant_key", "value for the constant key"); if(defined("constant_key")){ echo "constant_key is defined"; }else{ echo "constant_key is not defined"; } ?>
輸出:
示例3:在沒有定義常量的情況下檢查是否滿足條件。
<?php //define("constant_key", "value for the constant key"); if(defined("constant_key")){ echo "constant_key is defined"; }else{ echo "constant_key is not defined"; } ?>
輸出:
推薦學習:《PHP視頻教程》