jscix Posted February 14, 2007 Share Posted February 14, 2007 How could I make this function return a true or false value depending on if the supplied string passes the checks?? <?php Function ValidateString($txtcheck) { //start function if ($txtcheck == (null)) { // didn't pass because string is null } elseif (strlen($txtcheck) < 3) { // didn't pass because string is too short } elseif (ctype_alnum(!$txtcheck)) { // didnt pass because string contains alphanumeric chars } else { // return true } } // end function sorry for the easy question, still getting the hang of this stuff. anyone? Quote Link to comment Share on other sites More sharing options...
zq29 Posted February 14, 2007 Share Posted February 14, 2007 Like so... <?php function ValidateString($txtcheck) { if($txtcheck == (null)) { return false; } elseif(strlen($txtcheck) < 3) { return false; } elseif(ctype_alnum(!$txtcheck)) { return false; } else { return true; } } ?> Quote Link to comment Share on other sites More sharing options...
jscix Posted February 14, 2007 Author Share Posted February 14, 2007 ok a bit embarressed, didn't think it'd be that easy ;;o thanks alot though! Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 14, 2007 Share Posted February 14, 2007 Kind of "wordy" though. how about just: function ValidateString($txtcheck) { if( $txtcheck == (null) || strlen($txtcheck) < 3 || ctype_alnum(!$txtcheck) ) { return false; } else { return true; } } Quote Link to comment Share on other sites More sharing options...
zq29 Posted February 14, 2007 Share Posted February 14, 2007 No problem, always worth just typing it into your editor and testing it, you never know, it might just be that easy! Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 15, 2007 Share Posted February 15, 2007 Ok, I'm a minimalist and on second thought I realized you could compress this down to a single line: function ValidateString($txtcheck) { return ( !( $txtcheck == (null) || strlen($txtcheck) < 3 || ctype_alnum(!$txtcheck) ) ); } Quote Link to comment Share on other sites More sharing options...
zq29 Posted February 15, 2007 Share Posted February 15, 2007 Ok, I'm a minimalist and on second thought I realized you could compress this down to a single line: Well done, though, I intentionally re-wrote the function to clearly display the logic, as the original poster appears to be new to the language Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 15, 2007 Share Posted February 15, 2007 Well done, though, I intentionally re-wrote the function to clearly display the logic, as the original poster appears to be new to the language Agreed. Which is why I would not have proposed the last bit of code as an initial solution. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.