Convert string to boolean in PHP
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $values = [ true, '1', 'on', 'On', 'ON', 'true', 'True', 'TRUE', 'yes', 'Yes', 'YES', false, 'false', 'False', 'FALSE', 'no', 'No', 'NO', 'off', 'Off', 'OFF', '', '0', 'foo', '0.0', 'NAN' ]; foreach ($values as $val) { var_export($val); echo ' => '; var_dump(filter_var($val, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)); } ?> |
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,
|
1 2 3 4 5 6 7 |
<?php $str_val = 'true'; $bool_val = ($str_val === 'true' || $str_val === true); var_dump($bool_val); // bool(true) ?> |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php $str_values = [true, '1', 'on', 'On', 'ON', 'true', 'True', 'TRUE', 'yes', 'Yes', 'YES', false, 'false', 'False', 'FALSE', 'no', 'No', 'NO', 'off', 'Off', 'OFF', '', '0', 'foo', '0.0', 'NAN' ]; $valid_strings = ['1', 'on', 'On', 'ON', 'true', 'True', 'TRUE', 'yes', 'Yes', 'YES']; foreach ($str_values as $str_val) { var_export($str_val); echo ' => '; var_dump(array_search($str_val, $valid_strings) !== false); } ?> |
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.
|
1 2 3 4 |
<?php var_dump(json_decode('true')); // bool(true) var_dump(json_decode('false')); // bool(false) ?> |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php var_dump(json_decode('True')); // NULL var_dump(json_decode('TRUE')); // NULL var_dump(json_decode('False')); // NULL var_dump(json_decode('FALSE')); // NULL var_dump(json_decode(strtolower('True'))); // bool(true) var_dump(json_decode(strtolower('TRUE'))); // bool(true) var_dump(json_decode(strtolower('False'))); // bool(false) var_dump(json_decode(strtolower('FALSE'))); // bool(false) ?> |
Also See:
That’s all about converting a string to a boolean in PHP.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)