';如果';语句不';工作不正常


'IF' statement doesn't work right?

当我打印出整数(1或0)时,所有迭代(在这种情况下有3次)都会打印出正确的数字。第一个和第二个返回1,最后一个返回0。但是,"if"语句似乎显示了其中的所有内容。我做错了什么?

下面的所有代码都在一个更大的"for"循环中。

$yn = 0;
if(!in_array($pos, $blocks)){
    $blocks[$x] = $pos;
    $x++;
    $yn = 1;
}
print_r($blocks);
print "YN: ".$yn; # this prints out 1, 1 and 0 on the last iteration
if(yn){
        # show some stuff (is displayed in all three iterations, but it shouldn't be on the last)
}

尝试:

if($yn){

PHP将yn解释为字符串,而不是您应该使用的变量。