Elegant solution for line-breaks (PHP)

eozzy picture eozzy · Apr 28, 2010 · Viewed 102.9k times · Source
$var = "Hi there"."<br/>"."Welcome to my website"."<br/>;"
echo $var;

Is there an elegant way to handle line-breaks in PHP? I'm not sure about other languages, but C++ has eol so something thats more readable and elegant to use?

Thanks

Answer

Pascal MARTIN picture Pascal MARTIN · Apr 28, 2010

For linebreaks, PHP as "\n" (see double quote strings) and PHP_EOL.

Here, you are using <br />, which is not a PHP line-break : it's an HTML linebreak.


Here, you can simplify what you posted (with HTML linebreaks) : no need for the strings concatenations : you can put everything in just one string, like this :

$var = "Hi there<br/>Welcome to my website<br/>";

Or, using PHP linebreaks :

$var = "Hi there\nWelcome to my website\n";

Note : you might also want to take a look at the nl2br() function, which inserts <br> before \n.