Friday, 13 July 2018

How To Evaluate String 'False' In PHP?

In PHP, falsy values such as the string "false" are evaluated as TRUE. In some cases, such as when making an HTTP request to the server, this is not the result one might expect. This happens because all values, including a boolean false, on client side are transported as a string. In this article we look at some ways to overcome this.
Before we begin, here's an overview of all the falsy values (values that evaluate to boolean false) in PHP:
ValueTypeExplanation
""StringEmpty String
0IntegerInteger Zero
0.0FloatFloat 0.0
"0"StringString "0"
NULLNULLNo Value
FALSEBooleanBoolean False
array()ArrayEmpty Array
$var;VariableUnset Variable
Anything other than these values will always return true when evaluated. It may be useful to mention here that casting to boolean type, or using the settype won't do the trick here because "false" (i.e. string false) is not type converted to boolean false. In fact it evaluates to true which is actually the correct interpretation. This is simply due to the fact that it is a non-empty string, that by definition is truthy. However, this may be problematic because a boolean false value transported to a PHP script would be received as a string "false" — not what one might expect in some cases.

Using 1 & 0 For Truthy & Falsy Values

A good workaround would be to transport boolean values to PHP as their integer counterparts 1 and 0 (as 1, of string or integer type, is considered a truthy value and 0, of string or integer type, is considered a falsy value). In such a case, the following would be true:
if("1") // true
if("0") // false

Using Loose Equality Comparison Operator (==)

Logically speaking, the string "false" and the string "true" both evaluate to boolean true. So the following are true:
if("TRUE" == TRUE) // true
if("FALSE" == FALSE) // false

if("0" == FALSE) // true
Since, the string false actually evaluates to boolean true, the loose equality comparison operator "FALSE" == FALSE would return false because TRUE == FALSE is false. In case of string false, this works perfectly fine as the evaluation results in boolean false (which is what we wanted), but keep in mind that for other falsy values such as the string 0, which evaluate to boolean false, would return true when "0" == FALSE because FALSE == FALSE is true.

Using json_decode()

Possible return values for this function are:
  • For a valid JSON string, the value encoded in JSON is returned in appropriate PHP type.
  • TRUEFALSE and NULL are returned as TRUEFALSE and NULLrespectively.
  • NULL is returned if the JSON string cannot be decoded.
Before we proceed, take a moment to go through the following rules when using json_decode:
// the name and value must be enclosed in double quotes
// single quotes are not valid 
$bad_json = "{ 'bar': 'baz' }";
json_decode($bad_json); // NULL

// the name must be enclosed in double quotes
$bad_json = '{ bar: "baz" }';
json_decode($bad_json); // NULL

// trailing commas are not allowed
$bad_json = '{ bar: "baz", }';
json_decode($bad_json); // NULL
In all of the cases above, the value returned is NULL because it's a malformed JSON string.
With all that information, consider the following:
json_decode("") // NULL
json_decode("0.0") // float(0)
json_decode("0") // int(0)
json_decode("NULL") // NULL
json_decode("FALSE") // bool(false)
json_decode("array()") // NULL

json_decode("TRUE") // bool(true)
Please note that this function only works with UTF-8 encoded strings.

Using filter_var()

Possible return values for this function are:
  • Returns TRUE for "1""true""on" and "yes"
  • Returns FALSE for "0""false""off" and "no"
  • Returns NULL otherwise.
With that information, consider the following:
filter_var("", FILTER_VALIDATE_BOOLEAN) // bool(false)
filter_var("0.0", FILTER_VALIDATE_BOOLEAN) // bool(false)
filter_var("0", FILTER_VALIDATE_BOOLEAN) // bool(false)
filter_var("NULL", FILTER_VALIDATE_BOOLEAN) // bool(false)
filter_var("FALSE", FILTER_VALIDATE_BOOLEAN) // bool(false)
filter_var("array()", FILTER_VALIDATE_BOOLEAN) // bool(false)

filter_var("TRUE", FILTER_VALIDATE_BOOLEAN) // bool(true)
filter_var(1, FILTER_VALIDATE_BOOLEAN) // bool(true)
filter_var("1", FILTER_VALIDATE_BOOLEAN) // bool(true)
filter_var("on", FILTER_VALIDATE_BOOLEAN) // bool(true)
filter_var("yes", FILTER_VALIDATE_BOOLEAN) // bool(true)
Important thing to remember with using this method is that it retuns TRUEonly for "1""true""on" and "yes", everything else returns FALSE.

0 comments:

Post a Comment