How to generate indented code?

Asked

Viewed 847 times

17

How to print indented code when I give one echo by PHP? let’s say the structure is 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 printing the result in a row.

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

there’s like imprint it indented in the source code?

  • How’s your code in PHP?

  • updated with the modification

  • you do not need to concatenate all lines, leave only the first and last quotes and remove the points

  • The space before the ' is not identado, should be ' <table>'.

  • You have checked if something in your application does not do this automatically?

  • @chocolatemontana Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? Need something to be improved?

Show 1 more comment

4 answers

15

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 the spaces within the string. What is outside is not printed. Note that to make jump line it was necessary to use the \n. And to use the \n had to use double quotes.

Or if you prefer (I prefer):

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

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • 3

    I see no reason to concatenate all lines @bigown

  • I don’t either, but who am I to define the style of each one. If I had something very problematic I would even suggest he do different. I think I should do it but it’s not fundamental. Maybe he has a reason to do it like this.

  • 1

    Actually @bigown The first case won’t be ident, it’ll be like <div> <table> <tr> all on the same line, missing a \n or PHP_EOL.

  • @Kaduamaral is true, I will modify.

  • 1

    Why use . in the echo instead of ,? I agree, the second example is preferable.

  • 1

    @Guilhermenascimento pq he used :P In fact as I had to change his way, I could have changed that too, but here’s the tip. With the comma you will avoid the cost of concatenation, which is not important in this specific case.

Show 1 more comment

7

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

In Google Chrome, press F12 and will open at the bottom the indented code correctly:

Developers Tool

Now if you need to generate an already indented code, you need to make this clear to PHP, because the spaces in your code are not translated to the return of echo.

There are several ways to do this, as you can see in the various answers.

Another way (nothing fancy) to do this is to use the special characters \t (tabulation) and \n (new line):

<?php

echo 
    "<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 here the result.

Personally, I prefer to use heredocs as pointed out by the reply of @Kaduamaral.

7

A very good way to print HTML is by using strings in format Heredoc, which has the advantage of not needing to escape the quotes, example:

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

Warning: It is very important to check that the lock identifier line does not contain any other character, except possibly a semicolon (;). Which means that the identifier cannot be indented, and that there can be no spaces or tabs before or after the semicolon. [Read more]

You can also close the tags PHP and add your HTML smoothly, 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 */
?>
  • 1

    The second example is the best way to work and avoid syntax errors (although sometimes we get lost in {...}). +1

  • 1

    It’s true @Guilhermenascimento I also prefer the second, and when it has many keys and starts to complicate I like to comment } // Fecha if ($stmt) or something similar, but the Sublime Text underlines the closing key when placing the cursor on the opening key. : D

  • 1

    Good idea, another practice is to avoid over ifs or use views :) But let’s not talk too much about this because it would be another one for tips kk. Congratulations!

0

To attempt texts within a string you can use the combination of escape characters \t, which will add a tab for each group of \t. It’s the same effect as typing the TAB key on the keyboard.

To add line breaks we use the combination of escape characters \n or \r n 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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.