如何在PHP的echo中添加换行符? [英] How to add a line break within echo in PHP?

查看:639
本文介绍了如何在PHP的echo中添加换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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是换行符. /n不是.

\n

现在,如果您要在页面上回显字符串:

Now if you are trying to echo string to the page:

echo  "kings \n garden";

输出 将为:

output will be:

kings garden

您不会在换行中出现garden,因为PHP是服务器端语言,并且您将输出作为HTML发送,因此需要在HTML中创建换行符. HTML无法理解\n.为此,您需要使用 nl2br() 函数.

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.

它的作用是:

返回在所有换行符(\ r \ n,\ n \ r,\ n和\ r)之前插入<br /><br>的字符串.

echo  nl2br ("kings \n garden");

输出

kings
garden

注意,请确保您在回声/打印\n时使用双引号,否则它将被字面显示为\ n.因为php解释器会按原样使用

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.写入文本文件

现在,如果您回显文本文件,则只能使用\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);

输出将是:

kings
 garden

这篇关于如何在PHP的echo中添加换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆