0

Here's my function:

function checkForDuplicates($items, $checkitem) { $rows = explode("\n", $items); foreach($rows as $item) { if($item === $checkitem) return TRUE; } return FALSE; } 

I've confirmed that its returns are accurate and work properly. Here's where the function gets called and I run into an issue:

$email = sanitizeInput($_POST['email']); $email = strtolower($email); $emails = file_get_contents('emails.txt'); if(checkForDuplicates($emails, $email) == FALSE); { $emailFile = fopen('emails.txt','a') or die ('Sorry. Subscriptions are disabled for the time being.'); fwrite($emailFile, $email."\n"); fclose($emailFile); } 

No matter what I input, it writes to the file either way. I can't possibly understand why such a simple comparison isn't working.

3
  • 1
    Some actual sample values for $items and $checkitem` would be helpful. Otherwise there's not enough here to help you Commented Jul 8, 2016 at 20:45
  • Could be line-break related. You could try to do if (trim($item) === $checkitem) and see if the behaviour improves. Commented Jul 8, 2016 at 20:46
  • I should clarify, the function works properly and returns the proper TRUE and FALSE values. So, I've narrowed it down to a problem with the comparison in the implementation of the function... Commented Jul 8, 2016 at 20:48

1 Answer 1

3

Your problem comes from the trailing ";" here

if(checkForDuplicates($emails, $email) == FALSE); 

It boils down to an if with an empty statement.

The following block (file appending) is always executed because it is not part of the condition.

Sign up to request clarification or add additional context in comments.

1 Comment

Deleted the semi-colon and it worked perfectly. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.