PHP中的字符串比较

时间:2014-05-30 20:18:19

标签: php

所以我一直试图比较两个字符串。一个来自$ _POST方法,另一个来自文本文件。虽然当我打印它们时它们似乎是相同的,但我的strcmp()永远不会返回0意味着它们不相等。为什么我的strcmp()函数永远不会返回0?

以下是实际代码:

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

1 个答案:

答案 0 :(得分:1)

您的某个字符串中可能包含隐藏字符。尝试进行var转储。

$comparison = strcmp($porfavor,$lineName);
if($comparison != 0){
echo "<pre>";
var_dump($porfavor);
var_dump($lineName);
exit;
}

一些提示:

  1. 您可能需要在变量上使用trim()来计算空格;
  2. 如果情况有所不同,请考虑使用strtoupper()。请记住它的区分大小写。