if (defined('CONST_NAME')) {<BR> //do something <BR>}<BR>
变量检测则是使用isset,注意变量未声明或声明时赋值为NULL,isset均返回FALSE,如:
<BR>if (isset($var_name)) {<BR> //do something<BR>}<BR>
函数检测用function_exists,注意待检测的函数名也需要使用引号,如:
if (function_exists('fun_name')) {<BR> fun_name();<BR>}<BR>
先不说多了我们看一个实例
<?php <BR>/* 判断常量是否存在*/ <BR>if (defined('MYCONSTANT')) { <BR>echo MYCONSTANT; <BR>} <BR>//判断变量是否存在 <BR>if (isset($myvar)) { <BR>echo "存在变量$myvar."; <BR>} <BR>//判断函数是否存在 <BR>if (function_exists('imap_open')) { <BR>echo<div style="color:transparent">!本文来源gaodai.ma#com搞##代!^码网(</div><sup>搞gaodaima代码</sup> "存在函数imag_openn"; <BR>} else { <BR>echo "函数imag_open不存在n"; <BR>} <BR>?><BR>
function_exists判断函数是否存在
<BR><?php<BR>if (function_exists('test_func')) {<BR> echo "函数test_func存在";<BR>} else {<BR> echo "函数test_func不存在";<BR>}<BR>?><BR>
filter_has_var函数
filter_has_var() 函数检查是否存在指定输入类型的变量。
若成功,则返回 true,否则返回 false。
<?php<BR>if(!filter_has_var(INPUT_GET, "name"))<BR> {<BR> echo("Input type does not exist");<BR> }<BR>else<BR> {<BR> echo("Input type exists");<BR> }<BR>?> <BR>
输出为. Input type exists