什么是使用返回true或false职能的PHP最佳做法? [英] What is the PHP best practice for using functions that return true or false?
问题描述
用PHP玩后,我发现,返回true为1,假为空。
回声(5 == 5)//显示1
回声(5 == 4)//显示什么
在编写返回true或false的功能,什么是最好的做法,使用它们?
例如,
函数IsValidInput($输入){
如果($输入...){
返回true;
}
其他{
返回false;
}
}
这是使用功能的最佳方式?
如果(IsValidInput($输入)){
...
}
你会怎么写相反的功能?
IsBadInput($输入){
回报! IsValidInput($输入);
}
当你使用 ===
运营商?
用PHP玩后,我发现,返回true为1,假为空。
块引用>这是不正确的(没有双关语意)。 PHP,像许多其他语言,有truthy和falsy的价值观,它可以表现得像
TRUE
或FALSE
相对于其他的值。有这么beause PHP使用弱类型(与强类型)。它比较时它们自动转换不同类型的值,因此它可以最终比较相同类型的两个值。当你
呼应TRUE;在PHP
,回声
将一直输出字符串。但你传入一个布尔值,有前转换为字符串回声
可以做自己的工作。因此,TRUE
自动转换为字符串1
,而FALSE
转换为,
。
当你使用===运算符?
块引用>这虚弱,或松,打字是PHP使用两个相等运算符的原因,
==
和===
。您可以使用===
当你想确保你比较两个值不只是平等(或同等学历),而且同一类型的。在实践中:回声1 == TRUE; //呼应1,因为数字1是一个truthy值
回音1 === TRUE; //呼应,因为1和真不相同类型(整数,布尔值)
在编写返回true或false的功能,什么是最好的做法,使用它们?
块引用>要precise的时候可以,返回真正的布尔值
TRUE
或FALSE
。典型案例是由pfixed功能$ P $是
,如isValidInput
。人们通常希望这样的功能恢复为TRUE
或FALSE
。在另一方面,这是非常有用的功能,你在某些情况下返回falsy或truthy的价值观。以
strpos
, 例如。如果发现在零位串,它返回0
(INT),但如果未找到字符串,则返回FALSE
(布尔)。所以:$文字=这本书在桌子上;
回声(strpos($文本的)== FALSE)? 未找到:发现; //会显示未找到
回声(strpos($文本的)=== FALSE)? 未找到:发现; //回声发现After playing with PHP, I discovered that true is returned as 1 and false as null.
echo (5 == 5) // displays 1 echo (5 == 4) // displays nothing
When writing functions that return true or false, what are the best practices for using them?
For example,
function IsValidInput($input) { if ($input...) { return true; } else { return false; } }
Is this the best way to use the function?
if (IsValidInput($input)) { ... }
How would you write the opposite function?
IsBadInput($input) { return ! IsValidInput($input); }
When would you use the
===
operator?解决方案After playing with PHP, I discovered that true is returned as 1 and false as null.
That is not true (no pun intended). PHP, like many other languages, has "truthy" and "falsy" values, which can behave like
TRUE
orFALSE
when compared to other values.It is so beause PHP uses weak typing (vs. strong typing). It automatically converts different types of values when comparing them, so it can eventually compare two values of the same type. When you
echo TRUE;
in PHP,echo
will always output a string. But you passed it a boolean value, that has to be converted to a string beforeecho
can do its job. SoTRUE
is automatically converted to the string"1"
, whileFALSE
is converted to""
.When would you use the === operator?
This weak, or loose, typing is the reason PHP uses two equality operators,
==
and===
. You use===
when you want to make sure both values you are comparing are not just "equal" (or equivalent), but also of the same type. In practice:echo 1 == TRUE; // echoes "1", because the number 1 is a truthy value echo 1 === TRUE; // echoes "", because 1 and TRUE are not the same type (integer and boolean)
When writing functions that return true or false, what are the best practices for using them?
Be precise when you can, returning the actual boolean
TRUE
orFALSE
. Typical cases are functions prefixed byis
, likeisValidInput
. One usually expects such functions to return eitherTRUE
orFALSE
.On the other hand, it's useful to have your function return a "falsy" or "truthy" values in some cases. Take
strpos
, for example. If it finds the substring in position zero, it returns0
(int), but if the string is not found, it returnsFALSE
(bool). So:$text = "The book is on the table"; echo (strpos($text, "The") == FALSE) ? "Not found" : "Found"; // echoes "Not found" echo (strpos($text, "The") === FALSE) ? "Not found" : "Found"; // echoes "Found"
这篇关于什么是使用返回true或false职能的PHP最佳做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!