问题
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