Home > Article > Backend Development > What does isset mean in php
isset() is a built-in function in PHP used to detect whether a variable is declared, and its return value is of Boolean type. If the variable does not exist, it returns FALSE; if the variable exists and its value is NULL, it also returns FALSE; if the variable exists and its value is not NULL, it returns TRUE.
The operating environment of this tutorial: Windows 7 system, PHP version 7.1, DELL G3 computer
The isset() function in PHP is used for detection Whether the variable is declared and non-NULL. This function returns a Boolean value, i.e. true/false.
Format:
bool isset ( mixed var [, mixed var [, ...]] )
Return value:
If the variable does not exist, return FALSE
if If the variable exists and its value is NULL, FALSE will also be returned. variable, TRUE will be returned only when each single item meets the previous requirement, otherwise the result will be FALSE
Warning: isset() can only be used with variables, because passing any other parameters will cause a parsing error. If you want to check whether a constant has been set, use the defined() function.
<?php $var = ''; // 结果为 TRUE,所以后边的文本将被打印出来。 if (isset($var)) { echo "This var is set so I will print."; } // 在后边的例子中,我们将使用 var_dump 输出 isset() 的返回值。 // the return value of 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 ?>
This is also valid for elements in an array:
<?php $a = array ('test' => 1, 'hello' => NULL, 'pie' => array('a' => 'apple')); 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 // Checking deeper array values var_dump(isset($a['pie']['a'])); // TRUE var_dump(isset($a['pie']['b'])); // FALSE var_dump(isset($a['cake']['a']['b'])); // FALSE ?>
[Recommended learning: "
PHP Video Tutorial"]
The above is the detailed content of What does isset mean in php. For more information, please follow other related articles on the PHP Chinese website!