[英]IF condition doesn't work as expected
Little problem with this php code: 这个PHP代码的小问题:
$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. 我想将其与“坏”进行比较,并打印一个特定的图像(由$ bad和$ good确定)是否为真,第二个图像为假。 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($ arvio [$ muuttuja])给出输出字符串(28)“ image” <-image是真实图像,不是文本。
var_dump($arvostelu) gives output array(4) {[0]}=> string(4) "test" [1]=> string(4) "bad" [2]=> string(7) "testing" [3]=> string(1) "a"} var_dump($ arvostelu)给出输出array(4){[0]} => string(4)“ test” [1] => string(4)“ bad” [2] => string(7)“ testing” [ 3] =>字符串(1)“ a”}
string 1 is same as in if ($arvio[$muuttuja] === "bad") 字符串1与if($ arvio [$ muuttuja] ===“ bad”)中的字符串相同
I'm not sure entirely where the problem lies, but you might try this instead of the if then else? 我不确定问题出在哪里,但是您可以尝试使用它,而不是if if else?
$arvio[$muuttuja] = ( strtolower( trim( $arvio[$muuttuja] ) ) )==='bad' ? $bad : $good;
and perhaps after checking that the file exists you ought to use:- 也许在检查文件存在之后,您应该使用:-
clearstatcache()
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: 不好有3个字符,不是4个,请尝试:
if(trim($arvio[$muuttuja]) == 'bad')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.