phphtmlprintingechonowdoc

PHP Print and Echo HTML


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


Solution

  • 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;
    
    }