ISSUE :
 
Profile photo
Submitted by Ramesh Shrestha
on June 19th, 2011 at 20:39:24 EST

line break '\n' does not work in php

Hi,
I have a code which is suppose to be very simple but not working. I am trying to insert '\n' to my data but it not working. In the screen, I see literal '\n' instead of line break.
My code snippet:
<?php
echo 'this is test line \n with break';
?>


This displays everything in literal as "this is test line \n with break" instead of actually inserting line break for "\n".

Can someone please let me know what I am missing?


Profile photo
Replied by Tim Joseph
on June 19th, 2011 at 21:18:59 EST
This is because you have your string in single quotes ' ' . When you enclose your string inside single quote in php, it will maintain exact literal. However, if you enclose something inside double quotes " ", it will process the contents
For example, if you do something like:
echo "this is test line \n with break";

It will process "\n" as new line.

More detail on single quote vs double quote in php:
http://us2.php.net/manual/en/language.types.string.php
Accepted Answer
Accepted
 Answer

Profile photo
Replied by Tim Joseph
on June 19th, 2011 at 22:13:39 EST
More detail on single quote vs double quote in php:
http://us2.php.net/manual/en/language.types.string.php
Edited on June 19th, 2011 at 22:14:18 EST

Profile photo
Replied by Ramesh Shrestha
on June 19th, 2011 at 22:15:19 EST
Thanks. That was it. I used single quote. Changing it to double quote fixed my issue.