views:

49

answers:

3

very simple question. Lol i am embarassed to ask this cause i am usually very good with php but what are the ways to display html inside of php? for example:


<? if($flag): ?>
  <div>This will show is $flag is true </div>
<? endif; ?>

OR


<?
  if($flag)
    echo '<div>This will show is $flag is true </div>';
?>

I know that there are at least 2 other ways i just cannot remember them atm... Help is def. appreciated in advance!! =D

+1  A: 

You can also use a heredoc.

fire
+2  A: 

Here's how a heredoc could be used:

if($flag)
{
    echo <<<HTML
        <div>This will show if \$flag is true </div>
HTML;

}

If you don't want variable interpolation, you have to escape possible varnames as I have above. Alternatively, you can use a nowdoc with PHP 5.3 onwards:

if($flag)
{
    echo <<<'HTML'
        <div>This will show if $flag is true </div>
HTML;

}
Paul Dixon
YES! that was the one i was looking for! tanks man :)
Dick Savagewood
A: 

PHP also has a nowdoc syntax that works like heredoc, but similar to single quoted strings, these doc blocks do not get parsed.

Craige