PHP - how to create a newline character?

PhpString Interpolation

Php Problem Overview


In PHP I am trying to create a newline character:

echo $clientid;
echo ' ';
echo $lastname;
echo ' ';
echo '\r\n';

Afterwards I open the created file in Notepad and it writes the newline literally:

> 1 John Doe\r\n 1 John Doe\r\n 1 John Doe\r\n

I have tried many variations of the \r\n, but none work. Why isn't the newline turning into a newline?

Php Solutions


Solution 1 - Php

Only double quoted strings interpret the escape sequences \r and \n as '0x0D' and '0x0A' respectively, so you want:

"\r\n"

Single quoted strings, on the other hand, only know the escape sequences \\ and \'.

So unless you concatenate the single quoted string with a line break generated elsewhere (e. g., using double quoted string "\r\n" or using chr function chr(0x0D).chr(0x0A)), the only other way to have a line break within a single quoted string is to literally type it with your editor:

$s = 'some text before the line break
some text after';

Make sure to check your editor for its line break settings if you require some specific character sequence (\r\n for example).

Solution 2 - Php

Use the predefined PHP_EOL constant:

echo $clientid, ' ', $lastname, PHP_EOL;

The constant value will be set according to the line endings of the operating system where PHP is executing. On Linux, it will be "\n"; on Windows, it will be "\r\n".

Solution 3 - Php

The "echo" command in PHP sends the output to the browser as raw html so even if in double quotes the browser will not parse it into two lines because a newline character in HTML means nothing. That's why you need to either use:

echo [output text]."<br>";

when using "echo", or instead use fwrite...

fwrite([output text]."\n");

This will output HTML newline in place of "\n".

Solution 4 - Php

Use the constant PHP_EOL to get the right character no matter the platform.

http://us3.php.net/manual/en/reserved.constants.php

A simple usage example:

<?php
$data = 'First line' . PHP_EOL .
		'Second line' . PHP_EOL .
		'Third line';

file_put_contents("filename.txt", $data);
?>

Solution 5 - Php

Strings between double quotes "" interpolate, meaning they convert escaped characters to printable characters.

Strings between single quotes '' are literal, meaning they are treated exactly as the characters are typed in.

You can have both on the same line:

echo '$clientid $lastname ' . "\r\n";
echo "$clientid $lastname \r\n";

outputs:

$clientid $lastname
1 John Doe

Solution 6 - Php

You should use this:

"\n"

You also might wanna have a look at PHP EOL.

Solution 7 - Php

Actually \r\n is for the html side of the output. With those chars you can just create a newline in the html code to make it more readable:

echo "<html>First line \r\n Second line</html>";

will output:

<html>First line
Second line</html>

that viewing the page will be:

First line Second line

If you really meant this you have just to fix the single quote with the "" quote:

echo "\r\n";

Otherwise if you mean to split the text, in our sample 'First line' and 'Second line' you have to use the html code: <br />:

First line<br />Second line

that will output:

First line
Second line

Also it would be more readable if you replace the entire script with:

echo "$clientid $lastname \r\n";

Solution 8 - Php

w3school offered this way:

echo nl2br("One line.\n Another line.");

by use of this function you can do it..i tried other ways that said above but they wont help me..

Solution 9 - Php

Nothing was working for me.

PHP_EOL

. "\r\n";

$NEWLINE_RE = '/(\r\n)|\r|\n/'; // take care of all possible newline-encodings in input file
$var = preg_replace($NEWLINE_RE,'', $var);

Works for me:

$admin_email_Body = $admin_mail_body .'<br>' ."\r\n";
$admin_email_Body .= 'This is line 2 <br>' ."\r\n";
$admin_email_Body .= 'This is line 3 <br>' ."\r\n";

Solution 10 - Php

For some reason, every single post asking about newline escapes in PHP fails to mention the case that simply inserting a newline into single-quoted strings will do exactly what you think:

ex 1.

 echo 'foo\nbar';

Example 1 clearly does not print the desired result, however, while it is true you cannot escape a newline in single-quotes, you can have one:

ex 2.

 echo 'foo
 bar';

Example 2 has exactly the desired behavior. Unfortunately the newline that is inserted is operating system dependent. This usually isn't a problem, as web browsers/servers will correctly interpret the newline whether it is \r, \r\n, or \n.

Obviously this solution is not ideal if you plan to distribute the file through other means then a web browser and to multiple operating systems. In that case you should see one of the other answers.

note: using a feature rich text editor you should be able to insert a newline as a binary character(s) that represents a newline on a different operating system than the one editing the file. If all else fails, simply using a hex editor to insert the binary ascii character would do.

Solution 11 - Php

Use the PHP nl2br to get the newlines in a text string..

> $text = "Manu is a good boy.(Enter)He can code well. > > echo nl2br($text); > > > Result. > > Manu is a good boy. > > He can code well.

Solution 12 - Php

I have also tried this combination within both the single quotes and double quotes. But none has worked. Instead of using \n better use <br/> in the double quotes. Like this..

$variable = "and";
echo "part 1 $variable part 2<br/>";
echo "part 1 ".$variable." part 2";

Solution 13 - Php

Use chr (13) for carriage return and chr (10) for new line

echo $clientid;
echo ' ';
echo $lastname;
echo ' ';
echo chr (13). chr (10);

Solution 14 - Php

For any echo statements, I always use <br> inside double quotes.

Solution 15 - Php

You Can Try This.
<?php
   $content = str_replace(PHP_EOL, "<br>", $your_content);
 ?>
 
<p><?php echo($content); ?></p>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondavidjhpView Question on Stackoverflow
Solution 1 - PhpGumboView Answer on Stackoverflow
Solution 2 - PhpnnevalaView Answer on Stackoverflow
Solution 3 - Phpdavea0511View Answer on Stackoverflow
Solution 4 - PhpSalvi PascualView Answer on Stackoverflow
Solution 5 - PhpKevin S. MillerView Answer on Stackoverflow
Solution 6 - PhpWouter DorgeloView Answer on Stackoverflow
Solution 7 - PhpShoeView Answer on Stackoverflow
Solution 8 - Phpamin gholamiView Answer on Stackoverflow
Solution 9 - PhpMotahar HossainView Answer on Stackoverflow
Solution 10 - Phpuser2574384View Answer on Stackoverflow
Solution 11 - PhpManu R SView Answer on Stackoverflow
Solution 12 - PhpphphungerView Answer on Stackoverflow
Solution 13 - PhpAriful Islam RajibView Answer on Stackoverflow
Solution 14 - Phpmatthewpark319View Answer on Stackoverflow
Solution 15 - PhpTariqul_IslamView Answer on Stackoverflow