Programming Tutorials

Function to convert strings to strict booleans in PHP

By: oscar in PHP Tutorials on 2011-04-06  

Note this one only checks for string and defaults to the PHP (boolean) cast where e.g. -1 returns true, but you easily add some elseifs for other datatypes.

function toStrictBoolean ($_val, $_trueValues = array('yes', 'y', 'true'), $_forceLowercase = true)
if (is_string($_val)) {
return (in_array(
, $_trueValues)
} else {
return (boolean) $_val;

Add Comment

* Required information


No comments yet. Be the first!

Most Viewed Articles (in PHP )

Latest Articles (in PHP)