In PHP development, we often meet to provide predefined judgments about whether variables, constants or functions exist. Below I will introduce some commonly used application examples for judging whether constants, variables and functions exist.
To detect constants, use defined, and to define constants, use define. Note that the constants to be detected need to use quotation marks (either single or double), such as:
The code is as follows
代码如下 |
复制代码 |
if (defined('CONST_NAME')) {
//do something
}
|
|
Copy code
|
代码如下 |
复制代码 |
if (isset($var_name)) {
//do something
}
函数检测用function_exists,注意待检测的函数名也需要使用引号,如:
if (function_exists('fun_name')) {
fun_name();
}
|
if (defined('CONST_NAME')) {
//do something
}
代码如下 |
复制代码 |
/* 判断常量是否存在*/
if (defined('MYCONSTANT')) {
echo MYCONSTANT;
}
//判断变量是否存在
if (isset($myvar)) {
echo "存在变量$myvar.";
}
//判断函数是否存在
if (function_exists('imap_open')) {
echo "存在函数imag_openn";
} else {
echo "函数imag_open不存在n";
}
?>
|
代码如下 |
复制代码 |
if (function_exists('test_func')) {
echo "函数test_func存在";
} else {
echo "函数test_func不存在";
}
?>
|
Variable detection uses isset. Note that if the variable is not declared or is assigned a value of NULL when declared, isset returns FALSE, such as:
The code is as follows
|
Copy code
|
if (isset($var_name)) {
//do something
}
Use function_exists for function detection. Note that the function name to be detected also needs to use quotation marks, such as:
代码如下 |
复制代码 |
if(!filter_has_var(INPUT_GET, "name"))
{
echo("Input type does not exist");
}
else
{
echo("Input type exists");
}
?>
|
if (function_exists('fun_name')) {
fun_name();
} |
Without further ado, let’s look at an example
The code is as follows
|
Copy code
|
/* Determine whether the constant exists*/ <🎜>
if (defined('MYCONSTANT')) { <🎜>
echo MYCONSTANT; <🎜>
} <🎜>
//Determine whether the variable exists <🎜>
if (isset($myvar)) { <🎜>
echo "Variable $myvar."; <🎜>
} <🎜>
//Determine whether the function exists <🎜>
if (function_exists('imap_open')) { <🎜>
echo "Function imag_openn exists"; <🎜>
} else { <🎜>
echo "Function imag_open does not exist"; <🎜>
} <🎜>
?>
function_exists determines whether the function exists
The code is as follows
|
Copy code
|
if (function_exists('test_func')) {<🎜>
echo "Function test_func exists";<🎜>
} else {<🎜>
echo "Function test_func does not exist";<🎜>
}<🎜>
?>
filter_has_var function
The filter_has_var() function checks whether a variable of the specified input type exists.
If successful, return true, otherwise return false.
The code is as follows
|
Copy code
|
if(!filter_has_var(INPUT_GET, "name"))<🎜>
{<🎜>
echo("Input type does not exist");<🎜>
}<🎜>
else<🎜>
{<🎜>
echo("Input type exists");<🎜>
}<🎜>
?>
The output is. Input type exists
http://www.bkjia.com/PHPjc/631512.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631512.htmlTechArticleIn PHP development, we often meet to provide predefined judgment variables, constants or functions. Below I Let’s introduce some commonly used applications for judging whether constants, variables and functions exist...
|
|
|
|
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn