Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Options to maintain line breaks in PHP > HTML source code?

Tags:

html

php

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?

like image 252
unclepete Avatar asked May 16 '26 07:05

unclepete


1 Answers

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

like image 120
Asenar Avatar answered May 18 '26 20:05

Asenar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!