My Account   Contact Us    
  
» HomeProductsSupportDeveloper CommunityCompany
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> ";
}

?>


Content Management Powered by ActivEdit  ActivEdit Browser Based WYSIWYG HTML Editor

More Code Samples

Sign up for our newsletter: | Subscribe with RSS: RSS
© ActivSoftware 1999 to 2005 | Privacy Statement