1 Answer
  • 0
name Punditsdkoslkdosdkoskdo

How can I echo HTML in PHP?

I want to conditionally output HTML to generate a page, so what's the easiest way to echo multiline snippets of HTML in PHP 4+? Would I need to use a template framework like Smarty?

echo '<html>', "
"; // I'm sure there's a better way!
echo '<head>', "
echo '</head>', "
echo '<body>', "
echo '</body>', "
echo '</html>', "
  • 1

There are a few ways to echo HTML in PHP.

1. In between PHP tags
<?php if(condition){ ?>     <!-- HTML here --><?php } ?>
2. In an echo
if(condition){     echo "HTML here";}

With echos, if you wish to use double quotes in your HTML you must use single quote echos like so:

echo '<input type="text">';

Or you can escape them like so:

echo "<input type="text">";
  • 1
Reply Report