pocobueno1388 Posted September 27, 2010 Share Posted September 27, 2010 I have an IF statement that is constantly returning TRUE, even when it shouldn't be. Basically it checks if an array is empty. <?php $error = array(); //No blanks if ($street_addr == "" || $city == "" || $state == "" || $zip == "" || $phone1 == "" || $phone2 == "" || $phone3 == "" || $county == "") $error[] = "You left something blank!"; //validate zip if(!preg_match("/^[0-9]{5}$/", $zip)) $error[] = "The ZIP code must be a 5-digit number."; //validate phone number if (!is_numeric($phone1) || !is_numeric($phone2) || !is_numeric($phone3)) $error[] = "Phone can only contain digits"; if (strlen($phone1) < 3 || strlen($phone2) < 3 || strlen($phone3) < 4) $error[] = "Invalid phone number"; $count_errors = count($error); if ($count_errors > 0){ echo "ERROR:<br />"; foreach ($error as $err){ echo "-$err<br />"; } } else { //Other code } ?> So the first part of the IF statement is always executing. $count_errors DOES return 0 when printed out. Any help would be greatly appreciated. Thanks! Link to comment https://forums.phpfreaks.com/topic/214531-if-statement-always-returning-true-when-it-shouldnt/ Share on other sites More sharing options...
pocobueno1388 Posted September 27, 2010 Author Share Posted September 27, 2010 I'm sorry, the IF statement I'm talking about is this one <?php if ($count_errors > 0){ echo "ERROR:<br />"; foreach ($error as $err){ echo "-$err<br />"; } } else { //Other code } ?> Link to comment https://forums.phpfreaks.com/topic/214531-if-statement-always-returning-true-when-it-shouldnt/#findComment-1116339 Share on other sites More sharing options...
rwwd Posted September 27, 2010 Share Posted September 27, 2010 Couldn't you just do something like:- if(empty($error)){ //Array is empty/has no value } else{ //Array has value, do whatcha need to do! } Instead of foreaching, that seems to be a total waste of CPU parser power. Unless I have once again, missed the point again today, Mondays are getting worse... Rw Link to comment https://forums.phpfreaks.com/topic/214531-if-statement-always-returning-true-when-it-shouldnt/#findComment-1116422 Share on other sites More sharing options...
pocobueno1388 Posted September 27, 2010 Author Share Posted September 27, 2010 rwwd - That's actually what I originally had before I started trying to figure out the problem. I found what was going on. One of my other functions was generating the "error", it had nothing to do with that code. That's my Monday brain fart! Link to comment https://forums.phpfreaks.com/topic/214531-if-statement-always-returning-true-when-it-shouldnt/#findComment-1116440 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.