2009-11-21 52 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。

相关问题