解析错误:在...中期待“T_STRING”或“T_VARIABLE”或“T_NUM_STRING”?

时间:2010-04-29 20:22:54

标签: php radio-button syntax-error

我试图在echo中放入一个if语句但是这个解析错误出现了,是不是可以这样做?我应该使用heredoc吗?

echo "<input name='main_branch' type='radio' value='1' <?php if($restaurant['main_branch'] == 1) { echo "checked"; } ?> />Yes
<input name='main_branch' type='radio' value='0' <?php if($restaurant['main_branch'] == 0) { echo " checked"; } ?> />No";

3 个答案:

答案 0 :(得分:2)

您不能将<?php .. ?>放在echo语句中。您需要在外部设置变量并将其包含在echo "<input... $checked>";之内,或使用<?php标记。

答案 1 :(得分:0)

您可能希望将其分开以便于阅读,如下所示:

<?php

    echo "<input name='main_branch' type='radio' value='1' ";

    if($restaurant['main_branch'] == 1) { echo "checked"; } 

    echo " />Yes"
        ."<input name='main_branch' type='radio' value='0' ";

    if($restaurant['main_branch'] == 0) { echo " checked"; } 

    echo " />No";

?>

答案 2 :(得分:0)

webdestroya指出了这个问题,jaltiere给出了一个解决方案,我想用PHP的嵌入式功能提供另一个解决方案。

<input name="main_branch"
       type="radio"
       value="1"
       <?php if ($restaurant['main_branch'] == 1): ?>
           checked="checked"
       <?php endif; ?>
/> Yes

<input name="main_branch"
       type="radio"
       value="0"
       <?php if ($restaurant['main_branch'] == 0): ?>
            checked="checked"
       <?php endif; ?>
/> No