user3213722 user3213722 - 1 year ago 65
PHP Question

simple comparison in PHP not working

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");

No matter what I input, it writes to the file either way and I can't possibly understand why such a simple comparison isn't working.
Any insight would be appreciated as I truly have no idea what's going on.
Thanks in advance,

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download