Elegant solution for line-breaks (PHP) Elegant solution for line-breaks (PHP) php php

Elegant solution for line-breaks (PHP)


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.


I have defined this:

if (PHP_SAPI === 'cli'){   define( "LNBR", PHP_EOL);}else{   define( "LNBR", "<BR/>");}

After this use LNBR wherever I want to use \n.


in php line breaks we can use PHP_EOL (END of LINE) .it working as "\n"but it cannot be shown on the ht ml page .because we have to give HTML break to break the Line..

so you can use it using define

define ("EOL","<br>");

then you can call it