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!

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.