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?

3

4 Answers

\n is a line break. /n is not.


use of \n with

1. echo directly to page

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' 

2. write to text file

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 
1

You have to use br when using echo , like this :

echo "Thanks for your email" ."<br>". "Your orders details are below:" 

and it will work properly

2

The new line character is \n, like so:

echo __("Thanks for your email.\n<br />\n<br />Your order's details are below:", 'jigoshop'); 
2

You may want to try \r\n for carriage return / line feed

2