What is the PHP best practice for using functions

2020-03-23 02:16发布

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?

5条回答
Juvenile、少年°
2楼-- · 2020-03-23 02:50
  1. Sure. Unless you need it in a different sort of structure, e.g. a while loop.

  2. You never would. Always invert the normal function directly.

  3. When you need to differentiate false from 0, '', etc.

查看更多
劳资没心,怎么记你
3楼-- · 2020-03-23 02:54
function isValidInput($input) {
  return ($input...);
}

if(isValidInput($input))
  ...

if(!isValidInput($input)) // never rewrite inverse functions
  ...

if(isValidInput($input) === false) {
  // Only run if the function returned a boolean value `false`
  // Does the same thing as above, but with strict typing.
}
查看更多
我命由我不由天
4楼-- · 2020-03-23 02:59

After playing with PHP, I discovered that true is returned as 1 and false as null.

No.. true and false are returned as boolean true and false. When you echo output it must be cast to a string for display. As per the manual:

A boolean TRUE value is converted to the string "1". Boolean FALSE is converted to "" (the empty string). This allows conversion back and forth between boolean and string value.

As for the rest: that's fine, yes, yes, when you want exact type matches, to avoid type juggling in comparisons, e.g. "1" == true is true but "1" === true is false.

查看更多
神经病院院长
5楼-- · 2020-03-23 03:03
function isValidInput($input){
    return ($input ...);   // if your test returns true/false, just return that result
}

Your last example is missing an argument, otherwise fine:

function isBadInput($input){
    return !isValidInput($input);
}
查看更多
Fickle 薄情
6楼-- · 2020-03-23 03:08

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"
查看更多
登录 后发表回答