Search code examples
phphtmlindentation

Can i use {} in PHP for save my HTML indent


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?


Solution

  • 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.

    $html = 
    '<div class="a">
        <div class="b">
            <div class="c">
                Hello world
            </div>
        </div>
    </div>';
    

    2nd: In this format, intents are only in PHP file.

    $html = 
    '<div class="a">' .
        '<div class="b">' .
            '<div class="c">' .
                'Hello world' .
            '</div>' .
        '</div>' .
    '</div>';