简体   繁体   English

if($ val)vs if($ val!=“”)vs。if(!empty($ val)) - 哪一个?

[英]if($val) vs. if($val != “”) vs. if(!empty($val)) — which one?

I see a lot of people using a variety of different methods to check whether of a variable is empty, there really seems to be no consensus. 我看到很多人使用各种不同的方法来检查变量是否为空,确实似乎没有达成共识。 I've heard that if($foo) is exactly the same as if(!empty($foo)) or if($foo != "") . 我听说if($foo)if(!empty($foo))if($foo != "")完全相同。 Is this true? 这是真的?

I realize it's a really simple question, but I'd really like to know. 我意识到这是一个非常简单的问题,但我真的很想知道。 Are there any differences? 有什么不同吗? Which method should I use? 我应该使用哪种方法?

Difference between bare test and comparison to empty string 裸测试与空字符串比较的区别

if($foo != "") is equivalent to if($foo) most of the time, but not always . if($foo != "") if($foo) 大部分时间等于if($foo) ,但并非总是如此

To see where the differences are, consider the comparison operator behavior along with the conversion to string rules for the first case, and the conversion to boolean rules for the second case. 要查看差异的位置,请考虑比较运算符行为以及第一种情况下的字符串规则 转换以及第二种情况的转换为布尔规则

What I found out is that: 我发现的是:

  • if $foo === array() , the if($foo != "") test will succeed (arrays are "greater than" strings), but the if($foo) test will fail (empty arrays convert to boolean false ) 如果$foo === array()if($foo != "")测试将成功(数组是“大于”字符串),但if($foo)测试将失败(空数组转换为布尔值false
  • if $foo === "0" (a string), the if($foo != "") test will again succeed (obviously), but the if($foo) test will fail (the string "0" converts to boolean false ) 如果$foo === "0" (一个字符串), if($foo != "")测试将再次成功(显然),但if($foo)测试将失败(字符串"0"转换为布尔值false
  • if $foo is a SimpleXML object created from an empty tag, the if($foo != "") test will again succeed (objects are "greater than" strings), but the if($foo) test will fail (such objects convert to boolean false ) 如果$foo是一个从空标签创建的SimpleXML对象, if($foo != "")测试将再次成功(对象是“大于”字符串),但if($foo)测试将失败(这样的对象)转换为布尔值false

See the differences in action . 看看行动的差异

The better way to test 更好的测试方法

The preferred method to test is if(!empty($foo)) , which is not exactly equal to the above in that: 测试首选方法if(!empty($foo)) ,它与上面的完全相同:

  1. It does not suffer from the inconsistencies of if($foo != "") (which IMHO is simply horrible). 它不会受到if($foo != "") (IMHO简直太可怕)的不一致性的影响。
  2. It will not generate an E_NOTICE if $foo is not present in the current scope, which is its main advantage over if($foo) . 如果当前作用域中不存在$foo ,则不会生成E_NOTICE ,这是其优于if($foo)主要优势。

There's a caveat here though: if $foo === '0' (a string of length 1) then empty($foo) will return true , which usually is (but may not always be) what you want. 这里有一个警告:如果$foo === '0' (一个长度为1的字符串),那么empty($foo)将返回true ,这通常是(但可能并不总是 )你想要的。 This is also the case with if($foo) though. 这也是if($foo)的情况。

Sometimes you need to test with the identical operator 有时您需要使用相同的运算符进行测试

Finally, an exception to the above must be made when there is a specific type of value you want to test for. 最后,当您要测试特定类型的值时,必须对上述内容进行例外处理。 As an example, strpos might return 0 and also might return false . 例如, strpos可能返回0并且也可能返回false Both of these values will fail the if(strpos(...)) test, but they have totally different meanings. 这两个值都将失败if(strpos(...))测试,但它们具有完全不同的含义。 In these cases, a test with the identical operator is in order: if(strpos() === false) . 在这些情况下,使用相同运算符的测试依次为: if(strpos() === false)

No it's not always true. 不,并非总是如此。 When you do if($foo) PHP casts the variable to Boolean. 当你执行if($foo) PHP将变量转换为Boolean。 An empty string, a Zero integer or an empty array will then be false . 空字符串,零整数或空数组将为false Sometimes this can be an issue. 有时这可能是一个问题。

You should always try to use the most specific comparison as possible, if you're expecting a string which could be empty use if($foo==='') (note the three equal signs). 你应该总是尽量使用最具体的比较,如果你期望一个字符串可以是空的使用if($foo==='') (注意三个等号)。 If you're expecting either (boolean) false or a resource (from a DB query for instance) use if($foo===false){...} else {...} 如果您期望(布尔值)false或资源(例如来自数据库查询),请使用if($foo===false){...} else {...}

You may read the documentation about casting to boolean to find the answer to this question. 您可以阅读有关转换为布尔值文档以查找此问题的答案。 There's a list in there with which values are converted to true and false , respectively. 其中有一个列表,其值分别转换为truefalse

Note that empty also checks if the variable is set, which regular comparison does not. 请注意, empty也会检查变量是否已设置,而常规比较则不会。 An unset variable will trigger an error of type E_NOTICE during comparison, but not when using empty. 未设置的变量将在比较期间触发类型E_NOTICE的错误,但在使用空时不会触发。 You can work around this using the isset call before your comparison, like this: 您可以在比较之前使用isset调用解决此问题,如下所示:

if(isset($foo) && $foo != '')

if() "converts" the statement given to a bool, so taking a look at the documentation for boolean seems to be what you're looking for. if() “转换”给bool的语句,所以看一下boolean文档似乎就是你要找的东西。 in general: 一般来说:

  • empty strings ( "" ), empty arrays ( array() ), zero ( 0 ) and boolean false ( false ) are treated as false 空字符串( "" ),空数组( array() ),零( 0 )和布尔值false( false )被视为false
  • everything else ( "foo" , 1 , array('foo') , true , ...) is treated as true 其他一切( "foo"1array('foo')true ,...)被视为true

EDIT : 编辑:
for more information, you could also check the type comparison tables . 有关更多信息,您还可以检查类型比较表

empty($foo) should return true in all of these cases: 0,"", NULL. empty($ foo)应该在所有这些情况下返回true:0,“”,NULL。

For a more complete list check this page: http://php.net/manual/en/function.empty.php 有关更完整的列表,请查看此页面: http//php.net/manual/en/function.empty.php

No, it's not equal. 不,这不相等。 When variable is not defined, expression without empty will generate notice about non-defined variable. 如果未定义变量,则不带empty表达式将生成有关未定义变量的通知。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM