Line break

Question from a newbie. The documentation says that \r\nis an indication of the end of a line and a carriage return. Actually doesn’t work. I translate by /br. What is \n.



I asked about something else. The tutorial explicitly says:

echo "1 \n";
echo "2";



But it doesn’t work in practice. Writes in one line. That’s what I didn’t understand. What’s the catch.

Answer 1, authority 100%

If all your code is written for output through “\r\n” (By the way, it’s better to use PHP_EOLinstead, for cross-platform), then there is a wonderful function nl2br(), which will go through the line in which all the text generated by the page was written, and replace all line breaks with <br/>.

Answer 2, authority 75%

If you want to break lines on the page that is being displayed, use <br/>and there is also source code. The transfer there is carried out precisely by means of \n. Source code is formatted for readability.

Take a look at the source codeof this page. All these transfers are made by means of \nor by pressing Enter.

This code is in php

echo "1<br/>";
echo "2";

gives us


on the page and


in the file itself.

That’s the difference. I hope I understood your question correctly.

Answer 3, authority 50%

The newline result will be obtained when the text is output to a file or placed in <pre>or textarea. If you want to wrap text on the screen, use nl2br.
And in general, do not take the textbooks at their word. 😉

Answer 4, authority 38%

<br />is an HTML tag that tells the browser to display the content that follows it on a new line.

\r\n– normal line feed in text. There is also a cross-platform constant for line feed – PHP_EOL.

Line breaks in text are ignored by the browser (except in a few exceptions) and can only be seen if you open the page’s HTML code.

Answer 5, authority 12%

They said it was true about PHP_EOL, for example this code:
echo "hello",PHP_EOL;
echo "world"

will give

Answer 6, authority 12%

Look, I’m writing in the Clous9 console:

echo "\n";
echo "1: first_srt=1\nsecnd_srt=2\n";
echo "\n";
echo '2: first_srt=1\nsecnd_srt=2\n';
echo "\n";
echo "==============\n";
echo "\n";

and get:

 1: first_srt=1
  2: first_srt=1\nsecnd_srt=2\n

The difference between strings 1 and 2 is double and single quotes.

Answer 7, authority 12%

You need to put headerat the beginning of the code

header("Content-Type: text/plain; charset=utf-8");
echo " ...\n";
echo " ...\n";
echo " ...\n";