PHP-换行不起作用[重复]

This question already has an answer here:

I found that linebreak solution here

a) But the result of the echo has no linebreaks, what is wrong?

b) Can I do linebreaks in the echo as well?

PHP

<?php
$var = "Hi there
Welcome to my website
";
echo $var;
 ?>
</div>

Linebreak works when you run your script in console where this code means line break. Depending on the OS it can be , or . It will also work in web, in <PRE> block. Otherwise you must use <br /> or call nl2br() function on your string to tell PHP to do that for you.

Try this:

<?php
  $var = "Hi there
Welcome to my website
";
  echo nl2br($var);
 ?>

Line breaks in HTML are not rendered by browsers, so you'll need a <br/>-tag. the nl2br() function will provide that for you in case your text doesn´t have it before.

Use html break element <br/> to print line break. For detailed information look at MDN

Usually line breakings are used in CLI mode. I suppose you are using it through the web browser, so you can use HTML as it's primary used to control the view. $var = "Hi there <br /> Welcome to my website <br />";

you have different ways to do it.

First...

$var = "Hi there <br /> Welcome to my website <br /> ";
echo $var;

Second is

$var = "Hi there 
 Welcome to my website 
 ";
echo nl2br($var);

And also third one

$var = "Hi there ".PHP_EOL." Welcome to my website".PHP_EOL;
echo nl2br($var);

PHP_EOL is php constant and its value is .

get the description of nl2br http://pk1.php.net/nl2br

http://www.w3schools.com/php/func_string_nl2br.asp

And also see this it is very useful

http://www.go4expert.com/articles/difference-n-rn-t8021/

Remember: In file writing <br> doesn't work only will work means new line but if you are using html <br> tag is helpful.