How to generate indented code?

16

How to print indented code when I give echo to PHP? let's say the structure looks like this:

echo 
'<div>'.
    '<table>'.
        '<tr>'.
            '<td>'.
            '</td>'.
        '</tr>'.
    '</table>'.
'</div>';

When I go to see in the source code of the page, PHP ends up printing the result on a line.

<div><table><tr><td></td></tr></table></div>

Is there such a printa-lo indented in source code?

    
asked by anonymous 21.07.2015 / 22:41

4 answers

14

It's quite simple:

echo 
"<div>\n".
"    <table>\n".
"        <tr>\n".
"            <td>\n".
"            </td>\n".
"        </tr>\n".
"    </table>\n".
"</div>\n";

There are other ways to do this but I kept the pattern you used. The only modification is to include spaces within string . What is outside is not printed. Note that in order to skip line it was necessary to use \n . And to use \n had to use double quotation marks.

Or if you prefer (I prefer):

echo 
'<div>
    <table>
        <tr>
            <td>
            </td>
        </tr>
    </table>
</div>';

See running on ideone .

    
21.07.2015 / 23:14
7

A very good way to print your HTML is by using strings in the format Heredoc , which has the advantage of not having to escape the quotation marks, eg:

echo <<<EOT
<div>
    <table>
        <tr>
            <td>
                <a href="#" onclick="alert('oi');">oi</a>
            </td>
        </tr>
    </table>
</div>
EOT;
  

Warning:   It is very important to verify that the line in the close handle does not contain any other characters, except possibly a semicolon (;). This means that the handle can not be indented, and that there can be no space or tabs before or after the semicolon. [Read more]

You can also close the tags of PHP and add your html without problems, even in loops and conditions:

<?php
    $var = "Hello World";
?>
<div>
    <table>
    <?php 
        if (!empty($var) ){
            for ($i=0; $i < 3; $i++) { 
    ?>
        <tr>
            <td>
                <a href="#" onclick="alert('Oi');"><?php echo $var; ?></a>
            </td>
        </tr>
    <?php
            } // É essencial fechar as chaves do for
        }     // e do if 
    ?>
    </table>
</div>
<?php 
    /* Continua o código */
?>
    
21.07.2015 / 23:40
6

If your goal is just to view the source code, you can use the developer tools that comes with browsers.

In Google Chrome, press F12 and the right indented code will open at the bottom, and you can even navigate through it:)

Nowifyouneedtogenerateanalreadyindentedcode,youneedtomakeitcleartoPHP,becausethespacesinyourcodearenottranslatedtothereturnofecho.

Thereareseveralwaystodothis,asyoucanseeinthevariousanswers.

Anotherway(notelegant)todothisistousethespecialcharacters\t(tab)and\n(newline):

<?phpecho"<div>\n".
    "\t<table>\n".
    "\t\t<tr>\n".
    "\t\t\t<td>\n".
    "\t\t\t</td>\n".
    "\t\t</tr>\n".
    "\t</table>\n".
    "</div>\n";

See the result here .

I'd rather use heresocs as pointed out by @KaduAmaral's response and I believe that if you do not have a very very important reason , this approach should not be used as it greatly affects readability of the code.

    
23.07.2015 / 16:06
0

To attempt text within a string you can use the \t escape character combination, which will add a tab for each group of \t . It is the same as typing the TAB key on the keyboard.

To add line breaks, use the \ n or escape character combination in some cases.

For example, the string below

"<div>\n\t<p>\n\t\tIsso é um parágrafo\n\t</p>\n\<\div>"

is equivalent to the code below:

<div>
    <p>
        Isso é um parágrafo
    </p>
<\div>
    
29.07.2015 / 01:43