2016-02-12 69 views
2

我想弄清楚我應該如何構造一個if/else語句。PHP中的if/else結構

我明白基本if/esle應該如下;

<?php 
if ($a > $b) { 
    echo "a is bigger than b"; 
} elseif ($a == $b) { 
    echo "a is equal to b"; 
} else { 
    echo "a is smaller than b"; 
} 
?> 

我不知道如何實現這個邏輯到我目前的聲明。我認爲,因爲PHP是混合在HTML中它使我感到困惑:/

我敢肯定,這是非常錯誤的,但有人可以告訴我,它應該如何結構?

<form role="form"> 
    <div class="btn-group" data-toggle="buttons"> 
     <label class="btn btn-default <?php if ($status == '1') echo 'active btn-success' ; ?>"> 
     <input type="radio" name="status" id="option1" value="1" autocomplete="off"> Active 
     </label> 
     <label class="btn btn-default <?php elseif ($status == '2') echo 'active btn-warning' ; ?>"> 
     <input type="radio" name="status" id="option2" value="2" autocomplete="off"> Inactive 
     </label> 
     <label class="btn btn-default <?php else ($status == '3') echo 'active btn-danger' ; ?>"> 
     <input type="radio" name="status" id="option3" value="3" autocomplete="off"> Not Found 
     </label> 
    </div> 
    </form> 

我在NetBeans中看到的錯誤是;

Syntax error: unexpected elseif

我可以或應該只在每個標籤上使用if語句嗎?

+1

爲什麼'elseif'&'else'。在每個標籤中使用「if」。 –

+0

不完全確定,但您可能想使用if/elseif/else語句的開放/關閉括號,它可能會修復您的錯誤。 – Epodax

回答

5

Ravi Hirani has already answered the question,但我想我會解釋你爲什麼得到錯誤。

原因是您沒有使用正確的語法。如果我們拉出PHP和剝離的HTML,你也許能明白爲什麼:

​​

正如你所看到的,你只是缺少括號完成,如果在語句所需的語法。它應該看起來像(沒有標記):

if ($status == '1') { 
    echo 'active btn-success'; 
} elseif ($status == '2') { 
    echo 'active btn-warning'; 
} else ($status == '3') { 
    echo 'active btn-danger'; 
} 

現在,當包含HTML時,會變得相當凌亂!尤其是當試圖跟蹤多行標記的開始和結束括號時。所以我通常在使用標記時使用替代語法,只是爲了讓它更整潔一些!這可以在這裏找到:

http://php.net/manual/en/control-structures.alternative-syntax.php

所有你只需要做的就是換出了替代的角色括號:

if ($status == '1'): 
    echo 'active btn-success'; 
elseif ($status == '2'): 
    echo 'active btn-warning'; 
else ($status == '3'): 
    echo 'active btn-danger'; 
endif; 
+1

感謝您的解釋!我認爲替代角色是我想要了解的。這似乎是最好的辦法,因爲我有很多html :) –

+0

沒問題,很高興我可以幫助! :) – Othyn

+0

@Othyn:你有少女回答更可愛;) –

2

在每個標籤中只使用If條件。

<form role="form"> 
    <div class="btn-group" data-toggle="buttons"> 
     <label class="btn btn-default <?php if ($status == '1') echo 'active btn-success' ; ?>"> 
     <input type="radio" name="status" id="option1" value="1" autocomplete="off"> Active 
     </label> 
     <label class="btn btn-default <?php if ($status == '2') echo 'active btn-warning' ; ?>"> 
     <input type="radio" name="status" id="option2" value="2" autocomplete="off"> Inactive 
     </label> 
     <label class="btn btn-default <?php if ($status == '3') echo 'active btn-danger' ; ?>"> 
     <input type="radio" name="status" id="option3" value="3" autocomplete="off"> Not Found 
     </label> 
    </div> 
</form> 

你也可以通過下面的方式做到這一點,但不推薦。

<?php 
$var = ''; 
if ($status == '1'){ 
    $var = 'active btn-success'; 
}elseif($status == '2'){ 
    $var = 'active btn-success'; 
}else{ 
    $var = 'active btn-danger'; 
}  
?> 

<form role="form"> 
    <div class="btn-group" data-toggle="buttons"> 
     <label class="btn btn-default <?php echo $var; ?>"> 
     <input type="radio" name="status" id="option1" value="1" autocomplete="off"> Active 
     </label> 
     <label class="btn btn-default <?php echo $var; ?>"> 
     <input type="radio" name="status" id="option2" value="2" autocomplete="off"> Inactive 
     </label> 
     <label class="btn btn-default <?php echo $var; ?>"> 
     <input type="radio" name="status" id="option3" value="3" autocomplete="off"> Not Found 
     </label> 
    </div> 
</form> 

希望它會幫助你:)

2

爲什麼elseelseif。任何一方都只會設定你的狀況。

<form role="form"> 
    <div class="btn-group" data-toggle="buttons"> 
     <label class="btn btn-default <?php if ($status == '1') echo 'active btn-success' ; ?>"> 
     <input type="radio" name="status" id="option1" value="1" autocomplete="off"> Active 
     </label> 
     <label class="btn btn-default <?php if ($status == '2') echo 'active btn-warning' ; ?>"> 
     <input type="radio" name="status" id="option2" value="2" autocomplete="off"> Inactive 
     </label> 
     <label class="btn btn-default <?php if ($status == '3') echo 'active btn-danger' ; ?>"> 
     <input type="radio" name="status" id="option3" value="3" autocomplete="off"> Not Found 
     </label> 
    </div> 
</form> 
1

稍微不同的方法,這可能是一個ternary operator,它可以這樣使用:

<form role="form"> 
    <div class="btn-group" data-toggle="buttons"> 
     <label class="btn btn-default <?php echo ($status == '1') ? 'active btn-success' : ''; ?>"> 
     <input type="radio" name="status" id="option1" value="1" autocomplete="off"> Active 
     </label> 
     <label class="btn btn-default <?php echo ($status == '2') ? 'active btn-warning' : ''; ?>"> 
     <input type="radio" name="status" id="option2" value="2" autocomplete="off"> Inactive 
     </label> 
     <label class="btn btn-default <?php echo ($status == '3') ? 'active btn-danger' : ''; ?>"> 
     <input type="radio" name="status" id="option3" value="3" autocomplete="off"> Not Found 
     </label> 
    </div> 
</form> 

三元運營商基本上是一個快速的if/else。