I was trying to add a line break for a sentence, and I added /n
in following code.
echo "Thanks for your email. /n Your orders details are below:".PHP_EOL; echo 'Thanks for your email. /n Your orders details are below:'.PHP_EOL;
For some reasons, the I got server error as the result. How do I fix it?
\n
is a line break. /n
is not.
use of \n
with
Now if you are trying to echo string to the page:
echo "kings \n garden";
output will be:
kings garden
you won't get garden
in new line because PHP is a server-side language, and you are sending output as HTML, you need to create line breaks in HTML. HTML doesn't understand \n
. You need to use the nl2br()
function for that.
What it does is:
Returns string with
<br />
or<br>
inserted before all newlines (\r\n, \n\r, \n and \r).
echo nl2br ("kings \n garden");
Output
kings garden
Note Make sure you're echoing/printing
\n
in double quotes, else it will be rendered literally as \n. because php interpreter parse string in single quote with concept of as is
so "\n" not '\n'
Now if you echo to text file you can use just \n
and it will echo to a new line, like:
$myfile = fopen("test.txt", "w+") ; $txt = "kings \n garden"; fwrite($myfile, $txt); fclose($myfile);
output will be:
kings garden
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