使用返回 true 或 false 的函数的 PHP 最佳实践是什么? [英] What is the PHP best practice for using functions that return true or false?

查看:58
本文介绍了使用返回 true 或 false 的函数的 PHP 最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

玩过PHP后发现返回true为1,返回false为null.

echo (5 == 5)//显示 1echo (5 == 4)//什么都不显示

在编写返回 true 或 false 的函数时,使用它们的最佳实践是什么?

例如,

function IsValidInput($input) {如果($输入...){返回真;}别的 {返回假;}}

这是使用该功能的最佳方式吗?

if (IsValidInput($input)) {...}

你会如何编写相反的函数?

IsBadInput($input) {返回 !IsValidInput($输入);}

您什么时候使用 === 运算符?

解决方案

玩过PHP后发现返回true为1,返回false为null.

这不是真的(不是双关语).与许多其他语言一样,PHP 具有真"和假"值,与其他值相比,它们的行为类似于 TRUEFALSE.

之所以如此,是因为 PHP 使用弱类型(相对于 强类型).它在比较它们时会自动转换不同类型的值,因此它最终可以比较相同类型的两个值.当你在 PHP 中 echo TRUE; 时,echo 总是会输出一个字符串.但是你给它传递了一个布尔值,在 echo 可以完成它的工作之前,它必须被转换为一个字符串.所以 TRUE 会自动转换为字符串 "1",而 FALSE 会自动转换为 "".p><块引用>

什么时候使用 === 运算符?

这种弱或松散的类型是 PHP 使用两个相等运算符 ===== 的原因.=== 当您想确保所比较的两个值不仅相等"(或等价)而且属于同一类型时,您可以使用.在实践中:

echo 1 == TRUE;//回显1",因为数字 1 是一个真值回声 1 === 真;//回显",因为 1 和 TRUE 不是同一类型(整数和布尔值)

<块引用>

在编写返回 true 或 false 的函数时,使用它们的最佳实践是什么?

尽可能精确,返回实际的布尔值 TRUEFALSE.典型的情况是以is为前缀的函数,如isValidInput.人们通常期望这样的函数返回 TRUEFALSE.

另一方面,在某些情况下,让您的函数返回虚假"或真实"值很有用.以 strpos 为例.如果在位置 0 找到子字符串,则返回 0 (int),但如果未找到该字符串,则返回 FALSE (bool).所以:

$text = "书在桌子上";echo (strpos($text, "The") == FALSE) ?未找到":找到";//回显未找到"echo (strpos($text, "The") === 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 or FALSE 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 before echo can do its job. So TRUE is automatically converted to the string "1", while FALSE 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 or FALSE. Typical cases are functions prefixed by is, like isValidInput. One usually expects such functions to return either TRUE or FALSE.

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 returns 0 (int), but if the string is not found, it returns FALSE (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屋!

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