I write my HTML code in PHP syntax and need to save HTML indent
i use { … } for this job.
I’m afraid that is not the right way to save indentation
And it may not be supported in the next version or it may cause an error somewhere
Although I have never seen a problem
<?php
$html = '';
$html .= '<div class="a">';
{
$html .= '<div class="b">';
{
$html .= '<div class="c">';
{
$html .= 'Hello world';
}
$html .= '</div>';
}
$html .= '</div>';
}
$html .= '</div>';
echo $html;
?>
Can i use this way to save HTML indent?
3
Answers
Here is your example with PHP heredocs. You can read about them here in the official documentation.
Example:
Result:
There’s no problem using curly braces. In PHP they are useful when having
if
,while
etc. But I suggest two following formats that make your code more readable.1st: In this format, intents will include in the HTML file.
2nd: In this format, intents are only in PHP file.
It’s better to store html templates separately
This example will output HTML with text "Hello world":
You can also store generated html to variable using output buffering:
If you not want to use include, you can remember that php is essentially a template engine and write something like that: