在php中使用循环外部的循环中的变量

时间:2016-03-08 22:56:05

标签: php while-loop

我在while循环中有一个变量,即$confirm这个变量是动态的,可以是任何不同值的列表。

我想在循环外部使用此变量来检查它是否等于辅助变量

<?php 

$confirm = "";

while ($row_tutorunit = $tutorunit->fetch(PDO::FETCH_ASSOC)){
    $confirm .= $row_tutorunit["code"];
}

?>

用法;

<?php if ($confirm !== "$anothervariable"); { echo 'style="display:none;"';  }  ?>

因为我正在使用它,它似乎无法正常工作

1 个答案:

答案 0 :(得分:1)

if ($confirm !== $anotherVariable); { echo 'test'; }
//    I think your problem is here^

基本上,分散分号的作用是将if与应附加到其上的括号中的代码分开。实际上你在告诉PHP

if ($confirm !== $anotherVariable) {
    ; // Do nothing
}
echo 'test'; // Always

旁注,(假设$anothervariable是一个字符串)"$anothervariable" $anothervariable完全相同。报价是多余的。但'$anothervariable' $anothervariable相同,因为variable parsing适用于双引号字符串,但不适用于单引号字符串。