How come php process doesn't recongize the \n escape character开发者_开发知识库 in the string of this statement:
echo " testing \n testing2";
why?
I'm gonna go out on a limb here and guess that you wrote a page like this:
<?php
echo " testing \n testing2";
?>
And that the HTML output looks like:
testing testing2
Which is because HTML condenses white-space. You'll need to use some HTML elements to make the output look the way you think it should:
echo " testing <br /> testing2";
End of Line character means nothing in HTML, which is supposed to be PHP output.
The next line in HTML is <br/>
tag, So you should output that :
echo "<br/>";
You can also change all occurrences of \n
in a string to <br/>
via nl2br
function as noted by my friend.
When used in a HTML context, \n
linebreaks will not be visible except in the HTML sourcecode as HTML uses <br />
for linebreaks. To convert them, use nl2br()
.
You also need to be careful how your string is quoted. Escape sequences like \n
are only parsed in double-quoted "strings"
. In single-quoted 'strings'
they are not parsed.
It does, take a look in the source code. If you want to see it in the browser, use
echo nl2br(" testing \n testing2");
Browser ignores ignores excess whitespace (including a newline) and converts it to a space. You need HTML tags (e.g. <br />
) to manually break lines.
If you go to "View Source" in your browser, then you'll see that it has inserted a line break. However, in HTML, a line break is interpreted as whitespace and doesn't start a new line. As @Czechnology says, you want to use nl2br
to replace the newlines with HTML line breaks (<br>
).
Please also remember to accept the best answer when you ask a question.
Because your navigator think you want to display html whereas you want to display plain text. Just add a header explaining what type of content you want to display :
header('Content-Type: text/plain');
echo " testing \n testing2";
$teststring = ;
echo nl2br($teststring);
and type everything you want in $teststring
精彩评论