php not null if 语句的最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9073020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The best practice for not null if statements
提问by Evan Harrison
I've been writing my "If this variable is not empty" statements like so:
我一直在写我的“如果这个变量不是空的”语句,如下所示:
if ($var != '') {
// Yup
}
But I've asked if this is correct, it hasn't caused a problem for me. Here is the answer I found online:
但我已经问过这是否正确,它并没有给我带来问题。这是我在网上找到的答案:
if (!($error == NULL)) {
/// Yup
}
This actually looks longer than my approach, but is it better? If so, why?
这实际上看起来比我的方法更长,但它更好吗?如果是这样,为什么?
回答by webbiedave
Rather than:
而不是:
if (!($error == NULL))
Simply do:
简单地做:
if ($error)
One would think that the first is more clear, but it's actually more misleading. Here's why:
有人会认为第一个更清楚,但它实际上更具误导性。原因如下:
$error = null;
if (!($error == NULL)) {
echo 'not null';
}
This works as expected. However, the next five values will have the same and (to many, unexpected) behavior:
这按预期工作。但是,接下来的五个值将具有相同的(对许多人来说是意外的)行为:
$error = 0;
$error = array();
$error = false;
$error = '';
$error = 0.0;
The second conditional if ($error)
makes it more clear that type castingis involved.
第二个条件if ($error)
更清楚地表明涉及类型转换。
If the programmer wanted to require that the value actually be NULL
, he should have used a strict comparison, i.e., if ($error !== NULL)
如果程序员想要要求值实际上是NULL
,他应该使用严格的比较,即,if ($error !== NULL)
回答by Gerard ONeill
It is good to know exactly what is in your variable, especially if you are checking for uninitialized vs null or na vs true or false vs empty or 0.
确切地知道您的变量中有什么是很好的,特别是如果您正在检查未初始化与 null 或 na 与 true 或 false 与空或 0。
Therefore, as mentioned by webbiedave, if checking for null, use
因此,正如 webbiedave 所提到的,如果检查 null,请使用
$error !== null
$error === null
is_null($error)
if checking for initilized, as shibly said
如果检查初始化,正如 shably 所说
isset($var)
if checking for true or false, or 0, or empty string
如果检查 true 或 false,或 0,或空字符串
$var === true
$var === 0
$var === ""
I only use empty for ''s and nulls since string functions tend to be inconsistent. If checking for empty
我只对 ''s 和 null 使用空,因为字符串函数往往不一致。如果检查空
empty($var)
$var // in a boolean context
// This does the same as above, but is less clear because you are
// casting to false, which has the same values has empty, but perhaps
// may not one day. It is also easier to search for bugs where you
// meant to use ===
$var == false
If semantically uninitialized is the same as one of the values above, then initialize the variable at the beginning to that value.
如果语义上未初始化的值与上述值之一相同,则将开头的变量初始化为该值。
$var = ''
... //some code
if ($var === '') blah blah.
回答by dynamic
Why just don't
为什么不
if (!$var)
回答by shibly
There are ways:
有以下方法:
<?php
error_reporting(E_ALL);
$foo = NULL;
var_dump(is_null($inexistent), is_null($foo));
?>
Another:
其他:
<?php
$var = '';
// This will evaluate to TRUE so the text will be printed.
if (isset($var)) {
echo "This var is set so I will print.";
}
?>
To check if it's empty:
要检查它是否为空:
<?php
$var = 0;
// Evaluates to true because $var is empty
if (empty($var)) {
echo '$var is either 0, empty, or not set at all';
}
// Evaluates as true because $var is set
if (isset($var)) {
echo '$var is set even though it is empty';
}
?>