PHP only interprets escaped characters (with the exception of the escaped backslash \\ and the escaped single quote \') when in double quotes (")
This works (results in a newline):
This does not result in a newline:
Code:'\n' //NOT GOOD and WRONG
|
and
WRONG and OLD
Code:$unit1 = 'paragrahp1';
$unit2 = 'paragrahp2';
echo '<p>' . $unit1 . '</p>\n';
echo '<p>' . $unit2 . '</p>';
|
OLD and GOOD
Code:$unit1 = 'paragrahp1';
$unit2 = 'paragrahp2';
echo '<p>' . $unit1 . "</p>\n";
echo '<p>' . $unit2 . '</p>';
|
NEW and the BEST
Code:$unit1 = 'paragrahp1';
$unit2 = 'paragrahp2';
echo '<p>' , $unit1 , '</p>' , PHP_EOL;
echo '<p>' , $unit2 , '</p>' , PHP_EOL;
|
Learning
When do I use the PHP constant “PHP_EOL”?
PHP_EOL is ostensibly used to find the newline character in a cross-platform-compatible way, so it handles DOS/Mac/Unix issues.
Should it be used as the end-line character when writing a command-line script?
Yes it should!!
What is the benefit of \n and PHP_EOL in PHP?
A web browser interprets the output of a PHP program as HTML, so \n and \r\n will not appear to do anything, just like inserting a newline in an HTML file.
On the other hand, <br /> makes a new line in the interpreted HTML (hence "line BReak").
Therefore, <br /> will make new lines, whereas \r\n will not do anything.
OLD
NEW
OLD
Code:echo("<td align=\"center\" bgcolor=\"".$bgcolor2."\"><a href=\"" . $self . "&op=author_list\">" . _AUTHOR_LIST . "</a></td>\n");
|
NEW
Code:echo '<td align="center" bgcolor="', $bgcolor2, '"><a href="', $self, '&op=author_list">', _AUTHOR_LIST, '</a></td>', PHP_EOL;
|