This article demonstrates how to convert a string to a boolean in PHP.

1. Using filter_var() function

The filter_var() function filters a variable using a specified filter and returns the filtered data. To convert a string to a boolean, you can call filter_var() with the FILTER_VALIDATE_BOOLEAN filter. It returns true for string values like 1, true, on, and yes, and false otherwise. If FILTER_VALIDATE_BOOLEAN is used with the FILTER_NULL_ON_FAILURE flag, false is returned only for 0, false, off, no, '', and null is returned for everything else.

Download  Run Code

This results in below output:


true => bool(true)
'1' => bool(true)
'on' => bool(true)
'On' => bool(true)
'ON' => bool(true)
'true' => bool(true)
'True' => bool(true)
'TRUE' => bool(true)
'yes' => bool(true)
'Yes' => bool(true)
'YES' => bool(true)
false => bool(false)
'false' => bool(false)
'False' => bool(false)
'FALSE' => bool(false)
'no' => bool(false)
'No' => bool(false)
'NO' => bool(false)
'off' => bool(false)
'Off' => bool(false)
'OFF' => bool(false)
'' => bool(false)
'0' => bool(false)
'foo' => NULL
'0.0' => NULL
'NAN' => NULL

 
Note: As of PHP 8.0.0, you can use FILTER_VALIDATE_BOOL filter, which is an alias for FILTER_VALIDATE_BOOLEAN. Note that the case is ignored when filtering strings.

2. Using === operator

In PHP, strings are always evaluated to true unless they are falsey. In other words, using the equality operator, only two strings, '' and '0', return false when compared to the boolean true ('' == true, and '0' == true). However, the identity operator (===) can be used to obtain a boolean value based on a specific string value. For example,

Download  Run Code

 
Alternatively, you can create an array of all permissible strings, which evaluates to true, and then use the identity operator (===) to convert the given strings to boolean by searching for them in that array.

Download  Run Code

This results in below output:

true => bool(true)
'1' => bool(true)
'on' => bool(true)
'On' => bool(true)
'ON' => bool(true)
'true' => bool(true)
'True' => bool(true)
'TRUE' => bool(true)
'yes' => bool(true)
'Yes' => bool(true)
'YES' => bool(true)
false => bool(false)
'false' => bool(false)
'False' => bool(false)
'FALSE' => bool(false)
'no' => bool(false)
'No' => bool(false)
'NO' => bool(false)
'off' => bool(false)
'Off' => bool(false)
'OFF' => bool(false)
'' => bool(false)
'0' => bool(false)
'foo' => bool(false)
'0.0' => bool(false)
'NAN' => bool(false)

3. Using json_decode() function

If you just need to evaluate the strings 'true' and 'false', you can decode them using the json_decode() function. It returns boolean true and false only for their string representations, 'true' and 'false', respectively.

Download  Run Code

 
Note that the string should be valid JSON, otherwise, null is returned. That means json_decode() cannot decode strings like 'True' and 'False'. To make the conversion case-insensitive, you can convert the string to lowercase before passing it to the json_decode() function.

Download  Run Code

 
Also See:

Convert boolean to string in PHP

That’s all about converting a string to a boolean in PHP.