Home  >  Article  >  Backend Development  >  PHP determines whether a variable constant exists_PHP tutorial

PHP determines whether a variable constant exists_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:15:111158browse

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 "常量MYCONSTANT存在";
}else{
    echo "常量MYCONSTANT不存在";
}
echo "
";

if (defined('MYCONSTANT')) {
echo "Constant MYCONSTANT exists";

}else{

echo "Constant MYCONSTANT does not exist";
}
echo "
";

 代码如下 复制代码

$var = '';

if (isset($var)) {
print "This var is set set so I will print.";
}

// 在后边的例子中,我们将使用 var_dump函数 输出 isset() 的返回值。

$a = "test";
$b = "anothertest";

var_dump( isset($a) ); // TRUE
var_dump( isset ($a, $b) ); // TRUE

unset ($a);

var_dump( isset ($a) ); // FALSE
var_dump( isset ($a, $b) ); // FALSE

$foo = NULL;
var_dump( isset ($foo) ); // FALSE

?>

The isset function detects whether a variable is set.

1. If the variable does not exist, return FALSE
 代码如下 复制代码

$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' 等于 NULL,所以被认为是未赋值的。
// 如果想检测 NULL 键值,可以试试下边的方法。
var_dump( array_key_exists('hello', $a) ); // TRUE

?>

2. If the variable exists and its value is NULL, it also returns FALSE

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')) {
echo "函数test_func存在";
} else {
echo "函数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"))
{
echo("Input type does not exist");
}
else
{
echo("Input type exists");
}
?>

$a = "test"; $b = "anothertest"; var_dump( isset($a) ); // TRUE var_dump( isset ($a, $b) ); // TRUE unset ($a); var_dump( isset ($a) ); // FALSE var_dump( isset ($a, $b) ); // FALSE $foo = NULL; var_dump( isset ($foo) ); // FALSE ?> This also works for elements in an array:
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 ?>
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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/628881.htmlTechArticleWe have commonly used variables and constants in variable types in php. Let me introduce to you how to judge in php. Whether constants and variables already exist, friends who need to know can go to the reference...
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