3

I'd like to keep my HTML source code pretty and indented (or tabbed). For instance:

$someHTML.='<div class="NOINDENT">';
$someHTML.='    <div class="INDENT1">';
$someHTML.='        <div class="INDENT2"><div>';
$someHTML.='    </div>';
$someHTML.='</div>';

echo $someHTML;

This PHP looks pretty and readable enough to me, but when the HTML is output it will be one long line of code (with no line breaks)! I'd like to have PHP print a line break, but still have the code "readable" while working on it in PHP.

For the above example, is using \n at the end of each line my only option?

3
  • by the way, people do usually the opposite: trying to remove all invisible character (like new lines and tabs) in order to make their page lighter Commented Aug 19, 2014 at 16:00
  • @Asenar Yes I understand, that's called minify correct? I will be minifying other things like the CSS and possibly Javascript, but I would like the HTML to look as natural as possible (although I can't think of a good reason other than aesthetics). Commented Aug 19, 2014 at 21:19
  • yes for the name :) . That's my opinion, but I suggest you to focus on the look of your .php files, where you read it/change it often, more than the html output, where you will almost never look at it (in particular if your sources are clean). Of course the best solution would be to have an architecture which allows you to separate the "views" and "controller" code, or using a template engine Commented Aug 20, 2014 at 12:42

6 Answers 6

3

you can use the constant PHP_EOL to end your lines

$someHTML.='<div class="NOINDENT">'.PHP_EOL;

The HEREDOC syntax is better, but when you are inside functions / loop / etc. which requires tabs, it make the php code looks weird sometimes

Sign up to request clarification or add additional context in comments.

1 Comment

I kind of liek this better than using \n
3

Heredoc syntax to the rescue

$someHTML = <<<HTML
<div>
    <div>
        ...
    </div>
</div>
HTML;

Comments

2

I try and avoid echoing html if its more than a single line.

Instead you can use output buffering if you need the html in a string, eg if you need to return a string from a function:

function getHtmlString(){
    ob_start(); ?>

    <div class="NOINDENT">
        <div class="INDENT1">
            <div class="INDENT2"><div>
        </div>
    </div>

    <?php
    $html = ob_get_clean();
    return $html;
}
?>
<html>
<head>...</head>
<body>
    <div id="someid">
        <?php echo getHtmlString(); ?>
    ...

7 Comments

easier to just use a heredoc. no need to get the buffering system involved.
True in this specific case, but not so when logic blocks are required, as might appear in a full template
Trying to stay away from this , all the <?php and ?> can get too messy for my liking, even thought this is how I did it for years. However, the great advantage of using this way were that the HTML can be seen in something like Dreamweaver.
@unclepete personal choice i suppose, but i find doing the above, along with using templating php syntax eg foreach(..): endforeach; massivly more readable than echoing escaped html. Plus as you mention, it works a lot better with IDE's, so you can collopse sections, get syntax highlighting etc. Javascript is another plus - echoing escaped javascript is not fun
Great points user574632, I actually use a mix of the two (my example and yours). The reason I use the way in my example, is one long HTML "string" can be returned in a function. Not sure how HTML can be return()ed using this method.
|
1

Yes, you must add '\n' at the end.

And if you want tabs, add '\t' at the beggining of each line.

Comments

1

In order to use line breaks, you need to use the double-quoted string form and place a newline (\n) character at the end of every line. Note that the double quotes within the string now need to be escaped with a backslash. For example:

$someHTML.="<div class=\"NOINDENT\">\n";
$someHTML.="    <div class=\"INDENT1\">\n";
$someHTML.="        <div class=\"INDENT2\"><div>\n";
$someHTML.="    </div>\n";
$someHTML.="</div>\n";

echo $someHTML;

Another option would be to use the HEREDOC string format, which will maintain whitespace and also has the advantage of not requiring you to escape double quotes:

$someHTML = <<<HERE
<div class="NOINDENT">
    <div class="INDENT1">
        <div class="INDENT2"><div>
    </div>
</div>
HERE;

1 Comment

Thank you Andrew for this concise answer. I would like to vote this up but am 2 shy of that privelege.
0

I wanted to show that this solution is close to what I want , and works great, but the staring and ending ' and '; aren't that pretty. heredoc seems like the best and cleanest answer so far.

function html(){
    $someHTML=
'<div class="NOINDENT">a
    <div class="INDENT1">b
        <div class="INDENT2">c<div>
    </div>
</div>';
    return $someHTML;
}

echo(html());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.