2010-04-29 49 views
0

我試圖把一個if語句放在echo中,但是這個解析錯誤出現了,難道不可以這麼做嗎?我應該使用heredoc嗎?解析錯誤:期待`T_STRING'或`T_VARIABLE'或'T_NUM_STRING'in ..?

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"; 

回答

2

你不能把<?php .. ?>echo語句中。您需要在外面設置一個變量並將其包含在內,如echo "<input... $checked>";或使用<?php標記。

+0

非常感謝webdestroya – user306440 2010-04-29 20:39:57

+0

沒問題,樂於幫忙 – 2010-04-29 20:40:19

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"; 

?> 
+0

自從我寫了任何PHP以來,這已經有一段時間了,但我從來不是heredoc的忠實粉絲。 – jaltiere 2010-04-29 20:31:00

+0

感謝jaitiere的回覆:) – user306440 2010-04-29 20:40:51

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 
相關問題