PHP返回值的正确方法是
我有关于函数的返回值和检查变量值的非常基本的问题。
function test($var1, $var2){
if ($var1 == $var2){
$var3 = "abc";
return $var3;
}
return false
}
$value = test($var1, $var2);
if ($value){
echo "Value is".$value; //should output abc.
} else {
echo "Not equal";
}
返回值或者返回 false 可以吗?例如我没有返回 TRUE,可以吗?
当我调用该函数时,我将返回值存储在变量 $value 中。我如何检查函数是否返回了 $var3?应该使用哪个 if 条件?
if (!empty($value))
或if (isset($value))
或if ($value)
或if (value != false)
I have very basic question regarding return value of a function, and checking the variable value.
function test($var1, $var2){
if ($var1 == $var2){
$var3 = "abc";
return $var3;
}
return false
}
$value = test($var1, $var2);
if ($value){
echo "Value is".$value; //should output abc.
} else {
echo "Not equal";
}
Is it ok to either return a value or return false? For example I am not returning TRUE, it is ok?
When i call the function, i store the return value in a variable $value. How can i check the function did return the $var3? Which of the if condition should be used?
if (!empty($value))
orif (isset($value))
orif ($value)
orif (value != false)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
是的,PHP 中的常见做法是返回
FALSE
作为错误情况的指示符。 (什么构成错误是您自己的决定,并且取决于函数应该执行的操作。)但是,由于 PHP 会自动将值转换为其他类型的布尔值(例如空字符串或
0
,其计算结果也为FALSE
),您应该对FALSE
进行显式检查,如下所示:As Felix Kling评论中指出,这称为“严格比较”(或“身份比较”)。它检查值是否与
FALSE
相同,其中!= FALSE
、== FALSE
和if ($value)
仅检查某个值是否可以解释为FALSE
。Yes, it is common practice in PHP to return
FALSE
as an indicator of an error condition. (What constitutes an error is your own decision and depends on what the function is supposed to do.)However, since PHP automatically casts values to Boolean that are of another type (like the empty string or
0
, which evaluate toFALSE
as well), you should do an explicit check forFALSE
like this:As Felix Kling notes in the comments, this is called "strict comparison" (or "identity comparison"). It checks if a value is identical to
FALSE
, where as!= FALSE
,== FALSE
andif ($value)
only check if a value could be interpreted asFALSE
.我不是 PHP 开发人员,但我认为你的第一种方法行不通。
除了布尔值 false 之外,还有其他一些内容被解释为 false:
http://php. net/manual/en/language.types.boolean.php
I'm not a PHP developer, but I don't think your first approach works.
There are other things than the boolean value false interpreted as false:
http://php.net/manual/en/language.types.boolean.php
返回不同的数据类型是完全可以的。
如果要检查 false,请使用:if ($value !== false)。如果您不知道要使用哪个条件,这将澄清它: http://www .php.net/manual/en/types.comparisons.php
It's perfectly OK to return different data types.
If you want to check against false, use: if ($value !== false). If you get lost which condition to use, this will clarify it: http://www.php.net/manual/en/types.comparisons.php
您的函数返回
false
,所以我会进行该检查:if ($value != false)
Your function returns
false
, so I would go with that check:if ($value != false)
那是毫无意义的。您
返回
一个值,而不是一个变量。return "abc";
完全没问题。是的,对于像这样的简单案例来说,这完全没问题。
如上所述,该函数返回值
“abc”
,而不是$var3
。您将其保存在新变量$value
中。该变量肯定已设置(您刚刚在此处创建了它),因此不需要isset
或empty
。只需测试其值是true
还是false
(或者您想要测试的任何其他值)。所以你这样做的方式很好。That's pointless. You're
return
ing a value, not a variable.return "abc";
is perfectly fine.Yes, that's perfectly fine for a simple case such as this.
As said above, the function returns the value
"abc"
, not$var3
. You're saving it in a new variable$value
. This variable is definitely set (you just created it right there), so there's no need forisset
orempty
. Just test whether its value istrue
orfalse
(or whatever else you want to test for). So the way you're doing it in fine.是的,您可以从函数中返回几乎任何内容,或者您可以只“返回”而不返回任何内容。在您的示例中,您将得到一个字符串或“false”作为回报。
要检查 false,您可以执行 if (!$variable) 或 if ($variable===false)。如果您执行“if ($variable==false)”,则零将返回 true,因为自动将零转换为 false(以及任何其他正数转换为 true)。三个“= =”确保它确实是假的,没有别的。 isset($var) 检查是否存在,而不是值 - 并且不适用于您的示例,因为您的函数将返回一个值或“false”,因此始终存在。
Yes, you can return pretty much anything from the function, or you can just "return" without returning anything. In your example, you'll get a string or "false" in return.
To check for false you either do if (!$variable) or if ($variable===false). Zero will return true if you do "if ($variable==false)" due to auto casting of zero to false (and any other positive number to true). Three "===" makes sure it really is false and nothing else. The isset($var) checks for existance, not value - and is not applicable to your example since your function will return a value or "false" and thus always exists.
这里唯一正确的答案是:这取决于情况。
在创建这样的函数时,我总是问自己这个问题。为了回答这个问题,我分析了该函数的作用,而不是它返回的内容。
例如,如果我有一个 getter,我希望得到一个值,或者什么也得不到。在这种情况下,当没有找到任何内容/出现问题时,我经常返回
null
。在我看来,像您这样的测试函数应该始终返回布尔值。我认为,当您检查某些内容是真还是假时返回变量在语义上是不正确的。
除了语义之外:当您使用
if (test($var1, $var2)) 检查时,返回 0、false 或 null 并不重要,因为它们的工作方式都是相同的。但是,如果您想要一些更精细的细节,您需要进行身份检查 (
===
),而不是相等检查。在 PHP 中有时会出现这种情况,例如strpos
可以返回 0 或 false,0 表示找到匹配项,而 false 则未找到。因此,以下内容将失败:所以,长话短说:这取决于......
The only right answer here is: it depends.
I always ask myself this question when creating a function like this. To answer it, I analyze what the function does, instead of what it returns.
For instance, if I have a getter, I expect to get a value, or nothing. In this case I often return
null
when nothing is found/something went wrong.A test function like yours should return a boolean at all times, in my opinion. Returning a variable when you're checking for something to be true or false is semantically incorrect, I think.
Aside from the semantics: returning 0, false or null does not really matter when you're checking it with
if (test($var1, $var2))
, since it will all work the same. However, if you want some finer details, you want to do an identity check (===
) rather than a equality check. In PHP this is sometimes the case, for instancestrpos
can return 0 or false, 0 being a match is found, and false is not. Therefore the following would fail:So, long story short: it depends...