Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP won't compare same string

If I display $d[0] it is [email protected] but it is refusing to accept the if...

$d = file("mails.txt");
if ($d[0] == "[email protected]") {
    echo "JOW!";
}
echo $d[0];

Any idea?

like image 920
Liam Schnell Avatar asked Dec 06 '22 22:12

Liam Schnell


1 Answers

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])=="[email protected]"){
            echo "JOW!";
        }
        echo $d[0];

or not include any new lines at all:

        $d = file("mails.txt", FILE_IGNORE_NEW_LINES);
        if($d[0]=="[email protected]"){
            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

like image 130
Mike Lewis Avatar answered Dec 10 '22 11:12

Mike Lewis