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
Use nl2br()
to turn the new lines into HTML <br/>
elements:
<?php
echo nl2br("Hello World!\nSecond line");
?>
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).
在使用HTML打印时,应使用<br />
标记而不是\\n
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.