What is the best PHP practice for using functions returning true or false?

After playing with PHP, I found that true returns 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 methods for using them?

For instance,

 function IsValidInput($input) { if ($input...) { return true; } else { return false; } } 

Is this the best way to use this feature?

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

How could you write the opposite function?

 IsBadInput($input) { return ! IsValidInput($input); } 

When do you use the === operator?

+5
source share
5 answers

After playing with PHP, I found that true returns as 1 and false as null.

This is incorrect (pun intended). PHP, like many other languages, has “true” and “false” values ​​that can behave like TRUE or FALSE compared to other values.

This is because PHP uses weak typing (compared to strong typing ). It automatically converts different types of values ​​when comparing them, so it can ultimately compare two values ​​of the same type. When you echo TRUE; in PHP, echo always prints a string. But you passed it a boolean that should be converted to a string before echo can do its job. Therefore, TRUE automatically converted to string "1" , and FALSE converted to "" .

When do you use the === operator?

This weak or loose input type is the reason why PHP uses two equality operators == and === . You use === when you want to make sure that the two values ​​you are comparing are not just “equal” (or equivalent), but also of the same type. On 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 methods for using them?

Be precise when you can, returning the actual logical TRUE or FALSE . Typical cases are functions prefixed with is , for example, isValidInput . Typically, such functions usually return either TRUE or FALSE .

On the other hand, it is useful for your function to return "fake" or "true" values ​​in some cases. For example, strpos . If it finds the substring in the zero position, 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" 
+4
source
 function isValidInput($input){ return ($input ...); // if your test returns true/false, just return that result } 

There is no argument in your last example, otherwise fine:

 function isBadInput($input){ return !isValidInput($input); } 
+2
source
  • Sure. If you do not need it in another structure, for example. while loop.

  • You would never do that. Always invert the normal function directly.

  • If you need to distinguish false from 0 , '' etc.

+1
source

After playing with PHP, I found that true returns as 1 and false as null.

No. true and false returned as boolean true and false . When you echo it, it must be passed to a string to display. As in the manual :

The value boolean true converted to string "1" . Boolean false converts to "" (empty string). This allows you to convert back and forth between a boolean and a string value.

As for the rest: that's fine, yes, yes, when you want exact type matches to avoid type manipulation when comparing, for example. "1" == true true, but "1" === true is false.

+1
source
 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. } 
0
source

Source: https://habr.com/ru/post/987265/


All Articles