什么是使用返回true或false职能的PHP最佳做法? [英] What is the PHP best practice for using functions that return true or false?

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

问题描述

用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 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天全站免登陆