< BOOLEAN: Getting Column Type   (Previous) Table of Contents (Next)   BOOLEAN: Casting Results Example >

BOOLEAN: Casting Results

/**
 * Converts boolean-like input into true boolean values
 *
 * @param   mixed    $in   the boolean-like input to be converted
 * @return  boolean  true or false, depending upon the input.
 *                    Returns NULL if the input isn't boolean-like.
 */
function castToBoolean($in) {
    if (empty($bool_cast_types)) {
        $bool_cast_types = array(
            1       => true,
            't'     => true,
            'True'  => true,
            0       => false,
            'f'     => false,
            'False' => false,
        );
    }
    if (!isset($bool_cast_types[$in])) {
        if (empty($in)) {
            return null;
        }
        die('>' . $in . '< unknown datatype');
    }
    return $bool_cast_types[$in];
}