首页 > 编程开发 > PHP    日期: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()函数怎么用

觉得上面的内容有用吗?快来点个赞吧!

点赞() 我要打赏

温馨提示 : 本站内容来自会员投稿以及互联网,所有源码及教程均为作者总结编辑,请大家在使用过程中提前做好备份,以免发生无法预知的错误,源码类教程请勿直接用于生产环境!

 可能感兴趣的文章