Jump to content

If statement always returning TRUE when it shouldn't


pocobueno1388

Recommended Posts

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!

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
}

?>

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

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.