2009-11-21 55 views
0

這是可能的,並且它是使用PHP中的if語句中的heredoc進行編碼的正確方法嗎?是否有可能在if語句中使用heredoc?

if($selection!='') 
{ 
    $price=getselection($selection,$getprice[$selection]); 
    if ($selection<8) 
    { 
     print 'Please enter the amount<br />'; 
     print '<form action="" method="post"><input type="text" name="money1" value="'.$money1.'">'; 
     print '<input type="text" name="money2" value="'.$money2.'">'; 
     print '<input type="text" name="money3" value="'.$money3.'"><input type="submit">'; 
     print '<input type="hidden" name="selection" value="'.$selection.'"'; 
     print '</form><br>'; 
      if (($money1!='')&&($money2!='')&&($money3!=='')) 
      { 
       $total=$money1+$money2+$money3; 
       $money=getmoney($total); 
       $change=getchange($total,$price); 
      } 
     } 
    } 
echo '</pre>'; 

我試圖避免失控的PHP代碼,並跳進HTML,然後返回到PHP再次,我只是想保持對PHP腳本的一切;另外,使用多張打印紙很麻煩,謝謝你不要燃燒。

+1

試試它而不是問這個問題會更容易嗎? – 2009-11-21 05:34:12

+0

@Ben Shelock:我真的不明白爲什麼人們不願意嘗試一些東西。如果嘗試後它不起作用,那麼你會問這個問題。 – MitMaro 2009-11-21 05:49:08

回答

5

是否這樣?

if (conditional) 
{ 
    $foo = <<<html 
    <tag></tag> 
    <tag></tag> 
    <tag></tag> 
    <tag></tag> 
    <tag></tag> 
html; 
} 

我不知道任何情況下heredoc'不會工作'。只要始終注意確保heredoc的結束語句沒有主角。在我的示例中,html關閉了heredoc並且絕對沒有包含空格的主要字符。

+0

只想在行首的時候注意關於'html;'的問題。一個常見的錯誤是試圖使其與其他代碼保持一致。 – MitMaro 2009-11-21 05:27:24

+0

@MitMaro優秀的一點。我只是在發表評論時編輯了我的帖子。這可能是heredocs最容易被忽視和令人沮喪的方面。 – 2009-11-21 05:30:58

0

是的,您可以使用if語句中的heredocs。

相關問題