Skip to content

Instantly share code, notes, and snippets.

@pjdietz
Created June 12, 2013 17:53
Show Gist options
  • Save pjdietz/5767570 to your computer and use it in GitHub Desktop.
Save pjdietz/5767570 to your computer and use it in GitHub Desktop.
Interpret a human-readable string as a boolean in PHP
<?php
/**
* Convert a human-readable string to a boolean.
*
* Case insensitively treats true, t, yes, y, and 1 as true.
* Anything else is false.
*
* @param string $str
* @return bool
*/
function stringToBool($str)
{
$str = strtolower((string) $str);
return in_array(
$str,
array(
'true',
't',
'yes',
'y',
'1'
)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment