stringToBoolean()
Author:
Greg Alton
Category: PHP
Convert common user input to a boolean
<?php
/*
The stringToBoolean function accepts a string and returns a boolean. This is a nice function to handle user input for a boolean, which may be 0, false, False, no or No, for example.
*/
function stringToBoolean($str) {
$str = trim(strtolower($str));
if ($str == "0" || $str == "false" || $str == "no") {
return 0;
}else {
return 1;
}
}
// Code to test stringToBoolean()
$strings = array("false","no","test","string","False",0);
foreach($strings as $string) {
print "$string : ";
print stringToBoolean($string)? "True":"False";
print "<br>
";
}
?>
ActivEdit Browser Based WYSIWYG HTML Editor
More Code Samples