Asked  7 Months ago    Answers:  5   Viewed   32 times

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>', "n"; // I'm sure there's a better way!
echo '<head>', "n";
echo '</head>', "n";
echo '<body>', "n";
echo '</body>', "n";
echo '</html>', "n";

 Answers

17

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

3. Heredocs

4. Nowdocs (as of PHP 5.3.0)

Template engines are used for using PHP in documents that contain mostly HTML. In fact, PHP's original purpose was to be a templating language. That's why with PHP you can use things like short tags to echo variables (e.g. <?=$someVariable?>).

There are other template engines (such as Smarty, Twig, etc.) that make the syntax even more concise (e.g. {{someVariable}}).

The primary benefit of using a template engine is keeping the design (presentation logic) separate from the coding (business logic). It also makes the code cleaner and easier to maintain in the long run.

If you have any more questions feel free to leave a comment.

Further reading is available on these things in the PHP documentation.


NOTE: PHP short tags <? and ?> are discouraged because they are only available if enabled with short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option. They are available, regardless of settings from 5.4 onwards.

Tuesday, June 1, 2021
 
ammezie
answered 7 Months ago
40

You may have an apostrophe in the copied text and it's being converted into a curled apostrophe (see: http://www.dwheeler.com/essays/quotes-in-html.html). If you're just echoing it back out to the screen, you could wrap the $_POST['content'] in a htmlentities().

Saturday, May 29, 2021
 
EastSw
answered 7 Months ago
76

You can use http://html5boilerplate.com/ together with http://code.google.com/p/html5shiv/ and you're good to go.

For some other cool features, like video in old IE, you need to use workarounds like Flash for video and Javascript libs for canvas.

Wednesday, July 28, 2021
 
Drazisil
answered 5 Months ago
98

Put:

set echo and/or set verbose

at the top of your script. That will print out everything that happens in your script and are equivalent to the bash set -x and set -v commands.

Thursday, July 29, 2021
 
altexpape
answered 5 Months ago
68

You'll have to html encode your output

$string = "variable_name";
$tag_str = "<".$string.">";
echo htmlspecialchars($tag_str);
Thursday, August 5, 2021
 
leetwinski
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share