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? Link to comment https://forums.phpfreaks.com/topic/38535-help-with-a-function/ 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; } } ?> Link to comment https://forums.phpfreaks.com/topic/38535-help-with-a-function/#findComment-184952 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! Link to comment https://forums.phpfreaks.com/topic/38535-help-with-a-function/#findComment-184956 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; } } Link to comment https://forums.phpfreaks.com/topic/38535-help-with-a-function/#findComment-184959 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! Link to comment https://forums.phpfreaks.com/topic/38535-help-with-a-function/#findComment-184962 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) ) ); } Link to comment https://forums.phpfreaks.com/topic/38535-help-with-a-function/#findComment-185078 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 Link to comment https://forums.phpfreaks.com/topic/38535-help-with-a-function/#findComment-185243 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. Link to comment https://forums.phpfreaks.com/topic/38535-help-with-a-function/#findComment-185467 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.