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?
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:
when using "echo", or instead use fwrite...
This will output HTML newline in place of "\n".
Use the PHP nl2br to get the newlines in a text string..
You should use this:
You also might wanna have a look at PHP EOL.
Only double quoted strings interpret the escape sequences
\r
and\n
as '0x0D' and '0x0A' respectively, so you want: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 usingchr
functionchr(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:Make sure to check your editor for its line break settings if you require some specific character sequence (
\r\n
for example).Use chr (13) for carriage return and chr (10) for new line
w3school offered this way:
by use of this function you can do it..i tried other ways that said above but they wont help me..