这取决于您要查找的内容,如果您只是想查看它是否为空,那么只需使用
empty它即可检查它是否也已设置,是否想知道是否已设置了某些内容
isset。
Empty检查是否设置了变量,是否为null,“”,0等
Isset只是检查是否已设置,可能不是空
使用
empty,以下内容被认为是空的:
- “”(空字符串)
- 0(0为整数)
- 0.0(0为浮点数)
- “ 0”(0作为字符串)
- 空值
- 假
- array()(一个空数组)
- var $ var; (已声明变量,但类中没有值)
来自http://php.net/manual/en/function.empty.php
正如评论中提到的,对于empty(),缺少警告也很重要
PHP手册说
empty()与(boolean)var相反,不同之处在于 未设置变量时不生成警告 。
关于isset
PHP手册说
如果测试已设置为NULL的变量,则isset()将返回FALSE
您的代码可以满足以下要求:
<?php $var = '23'; if (!empty($var)){ echo 'not empty'; }else{ echo 'is not set or empty'; }?>例如:
$var = "";if(empty($var)) // true because "" is considered empty {...}if(isset($var)) //true because var is set {...}if(empty($otherVar)) //true because $otherVar is null {...}if(isset($otherVar)) //false because $otherVar is not set {...}


