我试图在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";
答案 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)
<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