Home >Backend Development >PHP Tutorial >PHP determines whether a variable constant exists_PHP tutorial
We have commonly used variables and constants in variable types in php. Now I will introduce to you how to determine whether constants and variables already exist in php. Friends who need to know more can enter for reference.
defined() function checks whether a constant exists.
If the constant exists, return true, otherwise return false.
The code is as follows | Copy code | ||||
if (defined('MYCONSTANT')) {
echo "Constant MYCONSTANT does not exist"; |
代码如下 | 复制代码 |
$var = ''; if (isset($var)) { // 在后边的例子中,我们将使用 var_dump函数 输出 isset() 的返回值。 $a = "test"; var_dump( isset($a) ); // TRUE unset ($a); var_dump( isset ($a) ); // FALSE $foo = NULL; ?> |
1. If the variable does not exist, return FALSE
代码如下 | 复制代码 |
$a = array ('test' => 1, 'hello' => NULL); var_dump( isset ($a['test') ); // TRUE // 'hello' 等于 NULL,所以被认为是未赋值的。 ?> |
3. If the variable exists and the value is not NULL, return TURE
4. When checking multiple variables at the same time, TRUE will be returned only when each single item meets the previous requirement, otherwise the result will be
The code is as follows | Copy code |
代码如下 | 复制代码 |
if (function_exists('test_func')) { |
$var = ''; if (isset($var)) {
print "This var is set set so I will print.";}
// In the following examples, we will use the var_dump function to output the return value of isset().
代码如下 | 复制代码 |
if(!filter_has_var(INPUT_GET, "name")) |
The code is as follows | Copy code |
<🎜>$a = array ('test' => 1, 'hello' => NULL); var_dump( isset ($a['test') ); // TRUE var_dump( isset ($a['foo') ); // FALSE var_dump( isset ($a['hello') ); // FALSE // 'hello' is equal to NULL, so it is considered unassigned. // If you want to detect NULL key values, you can try the following method. var_dump( array_key_exists('hello', $a) ); // TRUE ?> |
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";<🎜> }<🎜> ?> |
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