Line breaks in PhP


 
if ($error != '') {
  echo '<div id="error">'.$error.'</div>'."\
";
}
 
if ($error != '') {
  echo "<div id=\\"error\\">".$error."</div>\
";
}

I have seen the above done a few ways. Both work as intended, but is there any “Set in stone” way of using
?

It seems just because code works, doesn’t mean it is always the best way.

Is there a better way of adding
at the end of a sentence in order to make the output source correct and easier to read?

To ‘parse’ code in strings (
) you have to put it in double quotes ("), there is certainly no right or wrong way to do this, its down to personal preference.

I tend to go for the mixing and matching quotes myself, but i rarely print
etc at all.

I don’t think there’s one particular way that is better than the other. Go with what you feel comfortable with.

One more option:


if ($error != '') {
  echo '<div id="error">'.$error.'</div>' . chr(10);
}

:slight_smile:

This is the type of coding I like. It is clean and precise without jumping in and out of quotes/apostophes.

Is chr(10) safe to use in that way? Will it have adverse effects on different browsers or shortfalls in the future?

It can’t imagine it will, chr(10) just means a new-line character. As far as I know, it’s identical to "
"

Thanks for all the replies!

chr(10) is a “line feed” (
)
chr(13) is a “carriage return” (\r)