defined()是PHP中的内置函数,用于检查是否存在常量,即是否定义了常量;语法格式“defined(name)”,参数name是要检查的常量的名称。如果常量存在,则返回TRUE,否则返回FALSE。

本教程操作环境:windows7系统、PHP7.1版,DELL G3电脑 php defined()函数 defined() 函数检查常量是否存在。 语法 defined(name) name:规定要检查的常量的名称,不可省略。 返回值:如果常量存在,则返回 TRUE,否则返回 FALSE。 注意:此功能可用于PHP 4.0.0和更高版本。 示例1: <?php
define("constant_key", "value for the constant key");
echo defined("constant_key");
?>输出: 
示例2:定义常数后检查条件是否成立。 <?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视频教程》 以上就是php中defined()函数怎么用的详细内容,更多请关注模板之家(www.mb5.com.cn)其它相关文章! |