php中defined()函数怎么用

来自:互联网
时间:2021-04-01
阅读:

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"); 
?>

输出:

php中defined()函数怎么用

示例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"; 
} 
?>

输出:

php中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中defined()函数怎么用

返回顶部
顶部