2

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...

ilo
  • 316
  • 2
  • 12
  • 1
    stop chaining function calls that deal with external resources. your code assumes nothing could ever go wrong. `$temp = file_get_contents(...); verify_that_it_worked(); file_put_contents(...)`. – Marc B May 28 '15 at 18:20
  • In addition to the answers below, check: http://php.net/manual/en/language.operators.precedence.php. The `!` has precedence over `==` so `!$info['Poster']` will be evaluated first. – jeroen May 28 '15 at 18:22

2 Answers2

5

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") {
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Oh, my god... How could I miss that? But how could it work back then? – ilo May 28 '15 at 18:24
  • 2
    did you used to have it like this? `if(!($info['Poster'] == "N/A")) {` because that would have worked as well. – Jonathan May 28 '15 at 18:31
  • yes if you try if(!($info['Poster'] == "N/A")) this will also work but the best practice to do this is the way @john conde done – Aman Rawat May 28 '15 at 18:32
  • 1
    I agree @AmanRawat, I would personally never write it the way I commented above; however the OP said it used to work, my suspicion was those parentheses used to be there and were removed as it was thought they served no purpose. – Jonathan May 28 '15 at 18:36
  • Maybe it was like @AmanRawat said, I don't know. Maybe I was tired and deleted 'em... Anyways, thank you all for helping out a starter :) – ilo May 28 '15 at 18:41
1

Just move the ! from your condition, And it should work as expected. You are asking in your condition if $info['Poster'] is false, and it wont be false because it will have a string value. So, you are comparing a boolean value with a string value, false will be always different to "N/A:

        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;
        }
taxicala
  • 21,408
  • 7
  • 37
  • 66