开发者

PHP won't compare same string

开发者 https://www.devze.com 2023-02-21 04:43 出处:网络
If I display $d[0] it is liam.schnell@gmail.com but it is refusing to accept th开发者_如何学JAVAe if...

If I display $d[0] it is liam.schnell@gmail.com but it is refusing to accept th开发者_如何学JAVAe if...

$d = file("mails.txt");
if ($d[0] == "liam.schnell@gmail.com") {
    echo "JOW!";
}
echo $d[0];

Any idea?


Try calling the trim function on the $d[0], which will remove all new line characters at the beginning and end of the string.

        $d = file("mails.txt");
        if(trim($d[0])=="liam.schnell@gmail.com"){
            echo "JOW!";
        }
        echo $d[0];

or not include any new lines at all:

        $d = file("mails.txt", FILE_IGNORE_NEW_LINES);
        if($d[0]=="liam.schnell@gmail.com"){
            echo "JOW!";
        }
        echo $d[0];

Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used, so you still need to use rtrim() if you do not want the line ending present.

From: http://php.net/manual/en/function.file.php


I agree with Mike Lewis, using trim might fix why it was failing.

Also, in general, if you are having weird results in PHP when comparing strings, try '===' or strcomp to see that fixes the issue.

http://www.phpcatalyst.com/php-compare-strings.php


$d = file("mails.txt", FILE_IGNORE_NEW_LINES);
if ($d[0] == "liam.schnell@gmail.com") {
    echo "JOW!";
}
echo $d[0];
0

精彩评论

暂无评论...
验证码 换一张
取 消