26
$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

eozzy
  • 66,048
  • 104
  • 272
  • 428
  • possible duplicate of [How to remove line breaks (no characters!) from the string?](http://stackoverflow.com/questions/10757671/how-to-remove-line-breaks-no-characters-from-the-string) – kenorb Mar 03 '15 at 00:28

8 Answers8

45

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.

Vinod VT
  • 6,946
  • 11
  • 51
  • 75
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • 2
    `nl2br()` doesn't actually *convert* the newlines to `
    `. What it does is returns the given string with `
    ` or `
    ` inserted before all newlines.
    – Amal Murali Jan 03 '14 at 12:42
  • When my php returns "Hi there.\nWelcome." in an API call, then I log the output in my app, it displays as `Hi there.\nWelcome.`, with the \n intact (not converted to newline.) How do I get php to insert an actual \n newline char into the return result? (Oh! Does it have to do with the fact that the result is being `json_encode`-ed? Man, that's annoying! Ok, how to I fix *that*??) Thanks! – Olie Apr 22 '15 at 15:53
  • 2
    @Olie - json_encode should not matter. Double check you are using double quotes. When \n is inside single quotes it will be interpreted literally. – azoundria Feb 25 '16 at 11:22
6

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.

Daniel Beck
  • 6,363
  • 3
  • 35
  • 42
TheVyom
  • 533
  • 2
  • 11
  • 16
2

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

2

I ended up writing a function that has worked for me well so far:

// pretty print data
function out($data, $label = NULL) {

  $CLI = (php_sapi_name() === 'cli') ? 'cli' : '';

  $gettype = gettype($data);

  if (isset($label)) {
    if ($CLI) { $label = $label . ': '; }
    else { $label = '<b>'.$label.'</b>: '; }
  }

  if ($gettype == 'string' || $gettype == 'integer' || $gettype == 'double' || $gettype == 'boolean') {
    if ($CLI) { echo $label . $data . "\n"; }
    else { echo $label . $data . "<br/>"; }
  }
  else {
    if ($CLI) { echo $label . print_r($data,1) . "\n"; } 
    else { echo $label . "<pre>".print_r($data,1)."</pre>"; }
  }
}


// Usage

out('Hello world!');

$var = 'Hello Stackoverflow!';
out($var, 'Label');
eozzy
  • 66,048
  • 104
  • 272
  • 428
1

Not very "elegant" and kinda a waste, but if you really care what the code looks like you could make your own fancy flag and then do a str_replace.

Example:<br />
$myoutput =  "After this sentence there is a line break.<b>.|..</b> Here is a new line.";<br />
$myoutput =  str_replace(".|..","&lt;br />",$myoutput);<br />

or

how about:<br />
$myoutput =  "After this sentence there is a line break.<b>E(*)3</b> Here is a new line.";<br />
$myoutput =  str_replace("E(*)3","&lt;br />",$myoutput);<br />

I call the first method "middle finger style" and the second "goatse style".

Shehary
  • 9,926
  • 10
  • 42
  • 71
RandyMorris
  • 1,264
  • 9
  • 17
0

Because you are outputting to the browser, you have to use <br/>. Otherwise there is \n and \r or both combined.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
0

Well, as with any language there are several ways to do it.

As previous answerers have mentioned, "<br/>" is not a linebreak in the traditional sense, it's an HTML line break. I don't know of a built in PHP constant for this, but you can always define your own:

// Something like this, but call it whatever you like
const HTML_LINEBREAK = "<br/>";

If you're outputting a bunch of lines (from an array of strings for example), you can use it this way:

// Output an array of strings
$myStrings = Array('Line1','Line2','Line3');
echo implode(HTML_LINEBREAK,$myStrings);

However, generally speaking I would say avoid hard coding HTML inside your PHP echo/print statements. If you can keep the HTML outside of the code, it makes things much more flexible and maintainable in the long run.

mkgrunder
  • 941
  • 1
  • 7
  • 13
-1

\n didn't work for me. the \n appear in the bodytext of the email I was sending.. this is how I resolved it.

str_pad($input, 990); //so that the spaces will pad out to the 990 cut off.