if($val) vs. if($val != "") vs. if(!empty($val))——哪一个? [英] if($val) vs. if($val != "") vs. if(!empty($val)) -- which one?

查看:33
本文介绍了if($val) vs. if($val != "") vs. if(!empty($val))——哪一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到很多人使用各种不同的方法来检查变量是否为空,似乎确实没有达成共识.我听说 if($foo)if(!empty($foo))if($foo != "" 完全一样).这是真的吗?

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 != ""). 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?

推荐答案

裸测试与空字符串比较的区别

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.

我发现的是:

  • 如果 $foo === array()if($foo != "") 测试将成功(数组是大于"字符串),但是 if($foo) 测试将失败(空数组转换为布尔值 false)
  • 如果 $foo === "0" (一个字符串),if($foo != "") 测试会再次成功(显然),但是if($foo) 测试将失败(字符串 "0" 转换为布尔值 false)
  • 如果 $foo 是一个从空标签创建的 SimpleXML 对象,if($foo != "") 测试将再次成功(对象大于" 字符串),但 if($foo) 测试将失败(此类对象转换为布尔值 false)
  • 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)
  • 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)
  • 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)

查看不同之处.

首选测试方法if(!empty($foo)),它完全等于上面的:

The preferred method to test is if(!empty($foo)), which is not exactly equal to the above in that:

  1. 它不会受到 if($foo != "") 不一致的影响(恕我直言,这简直太可怕了).
  2. 如果 $foo 不在当前作用域中,它不会生成 E_NOTICE,这是它相对于 if($foo).
  1. It does not suffer from the inconsistencies of if($foo != "") (which IMHO is simply horrible).
  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 === '0'(长度为 1 的字符串)那么 empty($foo) 将返回 真的,这通常是(但不一定总是)你想要的.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. This is also the case with if($foo) though.

最后,当您要测试特定类型的值时,必须对上述情况进行例外处理.例如,strpos 可能返回 0,也可能返回 false.这两个值都将无法通过 if(strpos(...)) 测试,但它们具有完全不同的含义.在这些情况下,使用 相同运算符 的测试是有序的:<代码>if(strpos() === false).

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. Both of these values will fail the if(strpos(...)) test, but they have totally different meanings. In these cases, a test with the identical operator is in order: if(strpos() === false).

这篇关于if($val) vs. if($val != "") vs. if(!empty($val))——哪一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆