[英]PHP: My elseif statement is ignored
我的elseif
語句被忽略了。 if
和else
工作但是elseif
沒有。
if ($location === "Canterlot") {
echo "you cannot add a thread here...";
} elseif ($admin === "3") {
echo '<form action="" method="post">
<input type="text" name="thread_name" value="comment">
<textarea name="thread"></textarea>
<input type="submit" value="Comment"/>
</form>';
} else {
echo '<form action="" method="post">
<input type="text" name="thread_name" value="comment">
<textarea name="thread"></textarea>
<input type="submit" value="Comment"/>
</form>';
}
($admin === "3")
應該
($admin == "3")
注意
==運算符只是檢查左右值是否相等。 但是,===運算符實際上會檢查左右值是否相等,並檢查它們是否屬於同一變量類型。
我想你有這樣的事情:
$location = "Canterlot" and $admin ="3";
因此它將具有第一個條件為真( $location == "Canterlot"
)並且它永遠不會檢查else if ($admin =="3")
你可以嘗試這樣的事情:
if($location === "Canterlot" && $admin !== "3"){
echo "you cannot add a thread here...";
}else if ($admin === "3"){
echo '<form action="" method="post">
<input type="text" name="thread_name" value="comment">
<textarea name="thread"></textarea>
<input type="submit" value="Comment"/>
</form>';
}else{
echo '<form action="" method="post">
<input type="text" name="thread_name" value="comment">
<textarea name="thread"></textarea>
<input type="submit" value="Comment"/>
</form>';
}
更新:
如果$admin
是intteger,那么檢查如下:
if($location === "Canterlot" ){
echo "you cannot add a thread here...";
}else if ($admin == "3"){
echo '<form action="" method="post">
<input type="text" name="thread_name" value="comment">
<textarea name="thread"></textarea>
<input type="submit" value="Comment"/>
</form>';
}else{
echo '<form action="" method="post">
<input type="text" name="thread_name" value="comment">
<textarea name="thread"></textarea>
<input type="submit" value="Comment"/>
</form>';
}
我跑了這個:
<?php
$location = 'foo';
$admin = '3';
if ($location === "Canterlot") {
echo 'didn\'t get it';
} elseif ($admin === '3') {
echo 'got it';
} else {
echo 'didn\'t get it';
}
輸出got it
。 看到這里 。
附:
<?php
$location = 'foo';
$admin = 3;
if ($location === "Canterlot") {
echo 'didn\'t get it';
} elseif ($admin === 3) {
echo 'got it';
} else {
echo 'didn\'t get it';
}
輸出也是got it
。
請檢查您的其余代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.