I can write HTML code within a string and return it but it always looks a bit messy. Is there any better solution for this?
Example 1 - New line HTML
New line for every row with HTML. A very messy way to write HTML code.
function my_function()
{
$html = '<div class="hello">';
$html .= 'A very long text';
$html .= '</div>';
return $html;
}
Example 2 - HTML block
Still a messy way to write HTML code. Another downside is that it's harder to write 'cite' characters.
function my_function()
{
$html = '
<div class="hello">
A very long text
</div>';
return $html;
}
Example 3 - ob_get_contents
I could use ob_get_contents. It separates my PHP from my HTML, nice! But I have read that it is bad performance.
function my_function()
{
ob_start();
?>
<div class="hello">
A very long text
</div>';
<?php
$html = ob_get_contents();
ob_end_clean();
return $html;
}
Question
Is there another way to keep HTML from PHP in a nice way?