tags:

views:

99

answers:

2

Is it possible and is it the correct way to code using heredoc inside an if statement in php?

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

I am trying to avoid getting out of the php code, and hopping into html and then back into php again, I was just trying to keep everything on the php script; in addition, using multiple print's is messy, thank you for not flaming.

+4  A: 

Like this?

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

I'm not aware of any situation where a heredoc 'will not work'. Just always take care to ensure that the closing statement of the heredoc has no leading characters. In my example html closes the heredoc and has absolutely no leading chars including spaces.

Mike B
Just wanted to note about having `html;` at the very start of the line. A common mistake is to try to intent it to align with the rest of the code.
MitMaro
@MitMaro Excellent point. I edited my post just as you made your comment. That's probably the most overlooked and frustrating aspects of heredocs.
Mike B
A: 

Yes, you can use heredocs inside if statements.

Amber