How to Evaluate String 'false' in PHP?

Ways to evaluate a string 'false' as boolean 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:

Value Type Explanation
"" String Empty String
0 Integer Integer Zero
0.0 Float Float 0.0
"0" String String "0"
null null No Value
false Boolean Boolean False
array() Array Empty Array
$var; Variable Unset 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.
  • true, false and null are returned as true, false and null respectively.
  • 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('[]') // array(0) { }

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('[]', 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 true only for "1", "true", "on" and "yes", everything else returns false.


Hope you found this post useful. It was published (and was last revised ). Please show your love and support by sharing this post.