如何在PHP中的echo中添加换行符?

时间:2021-09-13 07:39:31

I was trying to add a line break for a sentence, and I added /n in following code.

我试图为句子添加换行符,并在下面的代码中添加了/ n。

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?

由于某些原因,我得到服务器错误的结果。我如何解决它?

5 个解决方案

#1


166  

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

\ n是一个换行符。 / n不是。


use of \n with

使用\ n和

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.

因为PHP是服务器端语言,并且您要将输出作为HTML发送,所以您不会在新行中获得花园,您需要在HTML中创建换行符。 HTML不明白\ n。你需要使用nl2br()函数。

What it does is:

它的作用是:

Returns string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r).

返回在所有换行符之前插入

的字符串(\ r \ n,\ n \ r \ n,\ n和\ r \ n)。

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

注意确保用双引号回显/打印\ n,否则它将按字面意思呈现为\ n。因为php解释器在单引号中解析字符串,其概念为as

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:

现在,如果您回显到文本文件,您只需使用\ n,它将回显到一个新行,如:

$myfile = fopen("test.txt", "w+")  ;

$txt = "kings \n garden";
fwrite($myfile, $txt);
fclose($myfile);

output will be:

输出将是:

kings
 garden

#2


12  

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

使用echo时必须使用br,如下所示:

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

and it will work properly

它会正常工作

#3


10  

The new line character is \n, like so:

新行字符是\ n,如下所示:

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

#4


2  

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

您可能想尝试\ r \ n进行回车/换行

#5


-7  

      <html>
      <body>
   <?php
      echo  "Hello World!" ;
      $tet =  4  ;
      echo "\n";
      $x = 5 + 5 ;
      $y =6;
      echo $y."\n number is :" .$x ;
      echo "\n";
      echo $tet ; 

      ?>

#1


166  

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

\ n是一个换行符。 / n不是。


use of \n with

使用\ n和

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.

因为PHP是服务器端语言,并且您要将输出作为HTML发送,所以您不会在新行中获得花园,您需要在HTML中创建换行符。 HTML不明白\ n。你需要使用nl2br()函数。

What it does is:

它的作用是:

Returns string with <br /> or <br> inserted before all newlines (\r\n, \n\r, \n and \r).

返回在所有换行符之前插入

的字符串(\ r \ n,\ n \ r \ n,\ n和\ r \ n)。

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

注意确保用双引号回显/打印\ n,否则它将按字面意思呈现为\ n。因为php解释器在单引号中解析字符串,其概念为as

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:

现在,如果您回显到文本文件,您只需使用\ n,它将回显到一个新行,如:

$myfile = fopen("test.txt", "w+")  ;

$txt = "kings \n garden";
fwrite($myfile, $txt);
fclose($myfile);

output will be:

输出将是:

kings
 garden

#2


12  

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

使用echo时必须使用br,如下所示:

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

and it will work properly

它会正常工作

#3


10  

The new line character is \n, like so:

新行字符是\ n,如下所示:

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

#4


2  

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

您可能想尝试\ r \ n进行回车/换行

#5


-7  

      <html>
      <body>
   <?php
      echo  "Hello World!" ;
      $tet =  4  ;
      echo "\n";
      $x = 5 + 5 ;
      $y =6;
      echo $y."\n number is :" .$x ;
      echo "\n";
      echo $tet ; 

      ?>