Search code examples
phpimdb

PHP Statement always return false


I have a little script witch fetches data from IMDB with omdbapi. I've managed to get the data from the site, but when I try to check if the movie's poster is valid, it always returns false.

            if(!$info['Poster'] == "N/A") {
                $url = $info['Poster'];
                $img = 'images/'.$info["imdbID"].'.jpg';
                file_put_contents($img, file_get_contents($url));
                echo 'Downloaded';
            } else {
                echo '!Downloaded';
                $noCover = true;
            }

The $info['Poster'] is containing data similar to this: http://ia.media-imdb.com/images/M/MV5BMTM0MDgwNjMyMl5BMl5BanBnXkFtZTcwNTg3NzAzMw@@._V1_SX300.jpg

It was working a while ago, but it somehow stopped...


Solution

  • Your if statement is written incorrectly. !$info['Poster'] means if $info['Poster'] is not true. If there is a value it will be translated to false as PHP's type juggling converts any non-empty string to true and the ! operator makes that false. false does not equal N/A as type juggling converts that to true (non-empty strings are always true). false is not equal to `true.

    You mean to use != which means not equal to

    if($info['Poster'] != "N/A") {