dsaba Posted November 30, 2007 Share Posted November 30, 2007 <?php function checkStr($str,$charArr) { //checks if chars of str are within charArr, returns true if it checks, false if it doesn't $flag = true; $x = -1; do { $x = $x + 1; $char = substr($str,$x,1); if (!in_array(strtolower($char),$charArr)) { $flag = false; echo $char; } } while($flag || $x < strlen($str)); return $flag; } $str = '133444444444444444444444444444444444'; if (checkStr($str,array('1','2','3','4'))) { echo 'yes'; } else { echo 'no'; } ?> Why doesn't it echo 'yes', I think it should -thanks Link to comment https://forums.phpfreaks.com/topic/79628-solved-why-doesnt-this-echo-yes-simple-function-test-bool/ Share on other sites More sharing options...
dsaba Posted December 1, 2007 Author Share Posted December 1, 2007 fixed changed "||" to "&&" solved Link to comment https://forums.phpfreaks.com/topic/79628-solved-why-doesnt-this-echo-yes-simple-function-test-bool/#findComment-403282 Share on other sites More sharing options...
ToonMariner Posted December 1, 2007 Share Posted December 1, 2007 <?php function checkStr($str,$charArr) { //checks if chars of str are within charArr, returns true if it checks, false if it doesn't $flag = true; $x = 0; do { if (!in_array(strtolower($str{$x}),$charArr)) { $flag = false; echo $str{$x}; break; } $x++; } while($x < strlen($str)); return $flag; } $str = '133444444444444444444444444444444444'; if (checkStr($str,array('1','2','3','4'))) { echo 'yes'; } else { echo 'no'; } ?> a slight;ly more elegant soluntion IMO... Link to comment https://forums.phpfreaks.com/topic/79628-solved-why-doesnt-this-echo-yes-simple-function-test-bool/#findComment-403286 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.