Skip to content

Instantly share code, notes, and snippets.

@pecuchet
Last active August 3, 2020 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pecuchet/9fb8ec5c4280705a0b4bbcfe31397caf to your computer and use it in GitHub Desktop.
Save pecuchet/9fb8ec5c4280705a0b4bbcfe31397caf to your computer and use it in GitHub Desktop.
Returns TRUE for "1", "true", "on" and "yes"; FALSE for "0", "false", "off", "no", and ""; NULL for all other values
<?php
if (!function_exists('cast_boolean')) {
/**
* Cast `'true'`, `'false'` etc. to bool.
* @param mixed $value
* @return bool
*/
function cast_boolean($value): bool
{
return filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment