Created
June 12, 2013 17:53
-
-
Save pjdietz/5767570 to your computer and use it in GitHub Desktop.
Interpret a human-readable string as a boolean in PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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