muti
muti

Reputation: 27

IF condition doesn't work as expected

Little problem with this php code:

$good = "<img src=./images/good.gif>";
$bad = "<img src=./images/bad.gif>";
while ($muuttuja <= $filecount) {
    $muuttuja = $muuttuja + 1;
    $arvostelutiedosto = "./moviefiles/$movie/review" . $muuttuja . ".txt";
    if (file_exists($arvostelutiedosto)) {
        $arvostelu = file($arvostelutiedosto);
        list($teksti[$muuttuja], $arvio[$muuttuja], $tekija[$muuttuja], $tyopaikka[$muuttuja]) = $arvostelu;
    }

        if ($arvio[$muuttuja] === "bad") {
            $arvio[$muuttuja] = $bad;
        } else {
            $arvio[$muuttuja] = $good;
        }

}

<?= $arvio[1] ?>
<?= $arvio[2] ?>
etc

I have files in that path, which is determined above. Second line on that file is "bad" or "good". I want to compare that with "bad" and print one specific image (which is determined by $bad and $good) if it is true and second one if it is false. Where do i have error (and by that i mean why it doesn't work)? Now it prints same image to every "review"

var_dump($arvio[$muuttuja]) gives output string(28)"image" <- image is real image, not text.

var_dump($arvostelu) gives output array(4) {[0]}=> string(4) "test" [1]=> string(4) "bad" [2]=> string(7) "testing" [3]=> string(1) "a"}

string 1 is same as in if ($arvio[$muuttuja] === "bad")

Upvotes: 1

Views: 90

Answers (2)

Adrian Cid Almaguer
Adrian Cid Almaguer

Reputation: 7791

You say:

var_dump($arvostelu) gives output array(4) {[0]}=> string(4) "test" [1]=> string(4) "bad" [2]=> string(7) "testing" [3]=> string(1) "a"}

string 1 is same as in if ($arvio[$muuttuja] === "bad")

but is not real, look at:

[1]=> string(4) "bad"

bad have 3 characters not 4, try with:

if(trim($arvio[$muuttuja]) == 'bad')

Upvotes: 0

Professor Abronsius
Professor Abronsius

Reputation: 33813

I'm not sure entirely where the problem lies, but you might try this instead of the if then else?

$arvio[$muuttuja] = ( strtolower(  trim( $arvio[$muuttuja] ) ) )==='bad' ? $bad : $good;

and perhaps after checking that the file exists you ought to use:-

clearstatcache()

Upvotes: 1

Related Questions