new lines in PHP. how?

随声附和 提交于 2021-02-04 21:08:53

问题


Code:

<?php

echo "Hello World!\nSecond line";

?>

I'm trying to display this as two separate lines but the line break character doesn't work and instead a space gets printed inbetween the two. I've tried \r\n as well as .PHP_EOL. as well as placing the string in single quotes. None of them seems to work. So how do I print new lines in PHP?

I'm working on phpDesigner 8


回答1:


Use nl2br() to turn the new lines into HTML <br/> elements:

<?php

echo nl2br("Hello World!\nSecond line");

?>    



回答2:


The linebreaks are actually created but you just don't see them. Change the Content-Type header to see the original result:

header('Content-Type: text/plain');

This is very useful when debugging. You could also view the page source in your browser to see the original output.

And to answer your question, the most easiest way to do this would be to use nl2br() (as has been suggested in John's answer above).




回答3:


As you are printing in HTML, you should use the <br /> tag instead of \n



来源:https://stackoverflow.com/questions/20148944/new-lines-in-php-how

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!