判断变量是否被定义: defined()
- if (defined('CONST_NAME')) {
- //do something
- }
判断变量是否存在: isset() , 注意变量未声明或声明时赋值为 NULL,isset 均返回 FALSE, 如:
- f (isset($var_name)) {
- //do something
- }
函数检测用 function_exists, 注意待检测的函数名也需要使用引号, 如:
- if (function_exists('fun_name')) {
- fun_name();
- }
实例:
- <?php
- /* 判断常量是否存在 */
- if (defined('MYCONSTANT')) {
- echo MYCONSTANT;
- }
- // 判断变量是否存在
- if (isset($myvar)) {
- echo "存在变量 $myvar.";
- }
- // 判断函数是否存在
- if (function_exists('imap_open')) {
- echo "存在函数 imag_openn";
- } else {
- echo "函数 imag_open 不存在 n";
- }
- ?>
function_exists 判断函数是否存在
- <?php
- if (function_exists('test_func')) {
- echo "函数 test_func 存在";
- } else {
- echo "函数 test_func 不存在";
- }
- ?>
filter_has_var 函数
filter_has_var() 函数检查是否存在指定输入类型的变量.
若成功, 则返回 true, 否则返回 false.
- <?php
- if(!filter_has_var(INPUT_GET, "name"))
- {
- echo("Input type does not exist");
- }
- else
- {
- echo("Input type exists");
- }
- ?>
输出为. Input type exists
来源: https://www.cnblogs.com/zsczsc/p/7089732.html