What does isset mean in php

青灯夜游
Release: 2023-03-08 16:48:01
Original
9124 people have browsed it

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.

What does isset mean in php

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 [, ...]] )
Copy after login

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

  • If a variable has been released using unset(), it will no longer be isset(). If you use isset() to test a variable that is set to NULL, it will return FALSE. Also note that a NULL byte ("\0") is not equivalent to PHP's NULL constant.
  • 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.

  • Example:
<?php

$var = &#39;&#39;;

// 结果为 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

?>
Copy after login

This is also valid for elements in an array:

<?php

$a = array (&#39;test&#39; => 1, &#39;hello&#39; => NULL, &#39;pie&#39; => array(&#39;a&#39; => &#39;apple&#39;));

var_dump(isset($a[&#39;test&#39;]));            // TRUE
var_dump(isset($a[&#39;foo&#39;]));             // FALSE
var_dump(isset($a[&#39;hello&#39;]));           // FALSE

// 键 &#39;hello&#39; 的值等于 NULL,所以被认为是未置值的。
// 如果想检测 NULL 键值,可以试试下边的方法。 
var_dump(array_key_exists(&#39;hello&#39;, $a)); // TRUE

// Checking deeper array values
var_dump(isset($a[&#39;pie&#39;][&#39;a&#39;]));        // TRUE
var_dump(isset($a[&#39;pie&#39;][&#39;b&#39;]));        // FALSE
var_dump(isset($a[&#39;cake&#39;][&#39;a&#39;][&#39;b&#39;]));  // FALSE

?>
Copy after login

[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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template