I am trying to write a PHP script that echo
s a string, but it doesn't recognize the end of line function in there.
For example,
echo "thank you \n " echo "for coming over"
The \n
is ignored and it prints the whole line as thank you for coming over
.
I got the same result for the following to:
echo "thank you " . PHP_EOL.; echo "for coming over" . PHP_EOL.;
?> Using new line tags: Newline characters \n or \r\n can be used to create a new line inside the source code.
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.
The nl2br() is an inbuilt function in PHP and is used to insert HTML break tags in the place of all new lines in a string.
The <br> HTML element produces a line break in text (carriage-return). It is useful for writing a poem or an address, where the division of lines is significant.
There are different ways to solve that:
echo '<pre>'; // pre-formatted text, \n should work echo "thank you \n "; echo "for coming over"; echo '</pre>';
header('Content-Type: text/plain'); echo "thank you \n "; echo "for coming over";
You browser will now properly read the output as text.
echo "thank you <br>\n "; echo "for coming over";
So output always depends on what you want to output. Is it text? Is it HTML? Is it some text within HTML? Depending on what you need you should take care on the format and formatting.
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