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?
It is a character in a string which represents a line break, which means that after this character, a new line will start. There are two basic new line characters: LF (character : \n, Unicode : U+000A, ASCII : 10, hex : 0x0a): This is simply the '\n' character which we all know from our early programming days.
Adding Newline Characters in a String In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.
\r is a Carriage Return \n is a Line Feed (or new line). On Windows systems these together make a newline (i.e. every time you press the enter button your fix will get a \r\n ). In PHP if you open a Windows style text file you will get \r\n at the end of paragraphs / lines were you've hit enter.
Apart from nl2br() function, a html break line tag </br> is used to break the string. The </br> tag should be enclosed in double quotes, e.g., "</br>". For Example. To create the newline in PHP, let's see the number of examples.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With