This question is an exact duplicate of:
So i have been trying to compare two strings. One from a $_POST method , and one read from a text file. Although they seem to be the same when I print both of them, my strcmp() never returns 0 meaning that they are not equal. Why does my strcmp() function never return 0?
Here is the actual code :
$fileRead = 'Users.txt';
$wasRead = FALSE;
$handleRead = fopen($fileRead,'r');
$character = fread($handleRead,1);
echo"<p> ".$character." </p>";
fgets($handleRead);
while($character != 'Q')
{
$lineName = fgets($handleRead);
echo "<p> ".$lineName." </p>";
$linePassword = fgets($handleRead);
echo "<p> ".$linePassword." </p>";
$character = fread($handleRead,1);
echo"<p> ".$character." </p>";
fgets($handleRead);
$porfavor = $_POST['newUserId'];
$porfavor = strtolower($porfavor);
echo $porfavor."<br>";
echo $lineName."<br>";
//$comparison = strcmp($lineName." ",$_POST['newUserId']);
//echo $comparison;
$comparison = strcmp($porfavor,$lineName);
echo $comparison;
if($comparison == 0)
{
$character = 'Q';
echo "<p> User Already Exists </p>";
echo "<a href = \" newUser.html\"> Sing In</a>";
echo "<br>";
}
}
fclose($handleRead);
</div>
You may have an invisible character in one of your strings. Try doing a var dump.
$comparison = strcmp($porfavor,$lineName);
if($comparison != 0){
echo "<pre>";
var_dump($porfavor);
var_dump($lineName);
exit;
}
some tips:
trim()
on your variables to account for whitespace;strtoupper()
if either might vary in case. Keep in mind it's case sensitive.