ErrorException [Parse Error]:语法错误,意外T_ENDIF

时间:2014-03-10 10:12:05

标签: php jquery kohana kohana-3

如果数据库中存在某些值,我想验证或显示选中的单选按钮。请参阅我的观点代码,

<input type="radio" name="bothadvandlor"  value="1" <?php if($advs->is_checked == "1");?>checked="checked"<?php endif;?> class="advs"/> Show Google Ads<br/>
<input type="radio" name="bothadvandlor"  value="2" <?php if($advs->is_checked == "2");?>checked="checked"<?php endif;?> class="advs"/> Others

以上是我尝试过的表单,但我收到的语法错误信息如“ErrorException [ Parse Error ]: syntax error, unexpected T_ENDIF”。

需要帮助来解决这个问题。

4 个答案:

答案 0 :(得分:3)

应为冒号:

if($advs->is_checked == "1"):
                            ^

Reference

答案 1 :(得分:2)

您需要在:条件后使用冒号;而不是分号if

if($advs->is_checked == "1"):
// -----------------------  ^ here

if($advs->is_checked == "2"):
// -----------------------  ^ and here   

来自 docs

  

PHP为其某些控制结构提供了另一种语法;   即,if,while,for,foreach和switch。在每种情况下,基本的   替代语法的形式是将左大括号更改为冒号   (:)和endif的结束括号;,endwhile;,endfor;,endforeach;,   或者endwitch;分别。

答案 2 :(得分:2)

<input type="radio" name="bothadvandlor"  value="1" <?php if($advs->is_checked == "1"):?>checked="checked"<?php endif;?> class="advs"/> Show Google Ads<br/>
<input type="radio" name="bothadvandlor"  value="2" <?php if($advs->is_checked == "2"):?>checked="checked"<?php endif;?> class="advs"/> Others

答案 3 :(得分:1)

我只想添加Kohana has got some neat stuff for this

  

public static radio(string $ name [,string $ value = NULL,boolean $ checked = bool FALSE,array $ attributes = NULL])

e.g。

<?php echo Form::radio('bothadvandlor', 1, ($advs->is_checked == '1'), array('class' => 'advs')); ?> Show Google Ads <br />

我认为这使得这些操作变得更加容易。更多的HTML和PHP分离,从而提高了可读性。

相关问题