PHP indentation in HTML

Asked

Viewed 326 times

0

Well, I wonder if there is a way to ident PHP along with HTML. For example, my code is like this:

<h1>Como identar código PHP no HTML?</h1>
<div>
    <?php
        echo "<p>
            <a href=\"#\">Testando</a>
        <p/>";
    ?>
</div>

Only when I run the page in the browser it looks like this:

<h1>Como identar código PHP no HTML?</h1>
<div>
    <p>
            <a href="#">Testando</a>
        <p/></div>

There is a way to view PHP in HTML?

  • use alternative syntax in these cases: https://www.php.net/manual/en/control-structures.alternative-syntax.php

  • 2

    First, it makes no sense to indent PHP within HTML, the indentation of PHP should be relative to PHP itself (i.e., its first code is poorly indented). And for HTML to come out right just put the indentation in echo. Example: echo "\t\t\t<p>estou indentado</p>\n"; - Three tabs and a line break in HTML (has a lot of ways. For large snippets can have HEREDOC and NOWDOC instead of echo, for example).

  • @Bacco Depending on the case (pq this author code probably isn’t even the one from the question) will probably solve better using only <a href="<?="variavel ou algo aqui";?>"><?="variavel ou algo aqui";?></a> and leave the indentation in the HTML itself

  • @Guilhermenascimento artificial example gives this, can not know or what is the problem actually has to solve (but anyway, indenting the PHP chunk by HTML never makes sense)

2 answers

1

Not much, you have to choose which one will make more sure the code you read every day, or the code that goes into the browser you never read. If you want to do the second you can do something like this:

<h1>Como identar código PHP no HTML?</h1>
<div>
    <?php
        echo "<p>
        <a href=\"#\">Testando</a>
    <p/>";
    ?>

</div>

I put in the Github for future reference.

You can also put PHP only where you need it, in which case you don’t even put it because there’s no real PHP code there. I understand it’s just an artificial example, but only with real examples can we make the right decisions. Each situation can ask for another way to do.

This case the solution is not to use PHP, but if to use then put the tag only at the specific point where there is code. Do not use echo to write whole blocks, just a very specific snippet. Or use only echo for everything, and forget the HTML feedback (I am not recommending, but it is a way to do).

And read the Bacco comment that makes sense. is mixing the indentations.

  • Out he could just use short tag ... <?="<p>a href="#">Testing</a> <p/>";?>

0


I understand you

It’s frustrating to see our code all messed up:D

Need indentation in HTML output?

You really don’t need.

Makes a difference to the execution of the code?

Dude if you care about every bit that runs, then you need to worry about the blanks. Or if you have a very large execution, it will weigh on the processing.

Someone’s gonna see that beautiful code?

If you look at the source code... Yes!

Looks good?

If you care about the presentation of the code, it will look beautiful!

Something positive?

  • You will feel good to have an easy-to-read HTML and without that sausage code ZONE.
  • When someone goes to look at your code you will see that you have cared about it and you will realize that if you even care about it for when someone goes to look, then you will worry about more important things.
  • Recruiters often look at code output to see if the professional is organized.

I know how you must feel. Without further ado.

You can use the HEREDOC

When I want an organized output of my HTML that will be written by PHP I do so:

<?php
echo <<<'HTML_EXEMPLO_PODE_SER_QUALQUER_NOME_AQUI'
    
    <h1> Precisa Olhar como ficou no comsole do Chrome </h1>
    <br>
        <p>
            <a href="#">Testando</a>
        <p/>
    <br>
            bem aqui, nessa posição, sem bagunça
HTML_EXEMPLO_PODE_SER_QUALQUER_NOME_AQUI;
?>

To see an example working online go to the following link and click the Run Code button, it’s the same code I put above. http://sandbox.onlinephpfunctions.com/code/7d0a80bbca28482554af71f877fe643c21b0ea76

Browser other questions tagged

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