How to string to boolean in PHP

Sometimes digesting JSON you may need to cast a string to boolean. Without trying yourself there is a function for this in PHP.

This is the way to do it:


filter_var('true', FILTER_VALIDATE_BOOLEAN); // true
filter_var('1', FILTER_VALIDATE_BOOLEAN); // true
filter_var('on', FILTER_VALIDATE_BOOLEAN); // true
filter_var('yes', FILTER_VALIDATE_BOOLEAN); // true

filter_var('false', FILTER_VALIDATE_BOOLEAN); // false
filter_var('0', FILTER_VALIDATE_BOOLEAN); // false
filter_var('off', FILTER_VALIDATE_BOOLEAN); // false
filter_var('no', FILTER_VALIDATE_BOOLEAN); // false
filter_var('', FILTER_VALIDATE_BOOLEAN); // false