Home  >  Article  >  Backend Development  >  What is the difference between isset() and empty() in php

What is the difference between isset() and empty() in php

王林
王林Original
2020-06-29 16:23:214519browse

The difference between isset() and empty() in php is: isset() function is used to detect whether the variable is set, the syntax format is: [isset (mixed var)]; empty() function is used to determine the value Whether it is empty, the syntax format is: [empty(mixed var)].

What is the difference between isset() and empty() in php

isset() function

isset() function is generally used to detect whether a variable is set.

Format:

bool isset ( mixed var [, mixed var [, ...]] )

Return value:

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 the value is not NULL, TRUE is returned;

When multiple variables are checked at the same time, TRUE is returned only when each single item meets the previous requirement, otherwise the result is FALSE .

empty() function

empty() function is used to determine whether the value is empty.

Format:

bool empty ( mixed var )

Return value:

If the variable does not exist, it returns TRUE;

If the variable exists and its value is "", 0, "0", NULL,, FALSE, array(), var $var; and objects without any attributes, return TRUE;

If the variable exists and the value is not "", 0, "0", NULL, FALSE, array(), var $var; and objects without any attributes, return FALSE.

Example: Simple comparison of empty() and isset()

<?php
$var = 0;
// 结果为 true,因为 $var 为空
if (empty($var)) {
echo &#39;$var is either 0 or not set at all&#39;;
}
// 结果为 false,因为 $var 已设置
if (!isset($var)) {
echo &#39;$var is not set at all&#39;;
}
?>

Note: empty() only detects variables, and detecting anything that is not a variable will cause a parsing error.

If you want to learn more related knowledge, please visit php Chinese website.

The above is the detailed content of What is the difference between isset() and empty() in php. For more information, please follow other related articles on the PHP Chinese website!

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