我的 PHP 换行初学者代码不会换行。帮助不大?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2257468/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
My PHP newline beginner code isn't going to a new line. Little help?
提问by Douwe Maan
Here's my code:
这是我的代码:
<html>
<Head>
<?php
$name = "Sergio";
?>
</Head>
<body>
<h1>It works!</h1>
<?php
echo "So someone tells me your name is " . $name . ".";
echo "Welcome to the site, " . $name . "\n";
echo "THEN WHO WAS NEW LINE?";
?>
</body>
</html>
Everything outputs to a single line with no newline. Any help?
一切都输出到一行,没有换行符。有什么帮助吗?
回答by Sarfraz
Use <br />because you are outputing on the browserwhich needs html tags.
使用<br />是因为您在需要 html 标签的浏览器上输出。
echo "So someone tells me your name is " . $name . "<br />";
echo "Welcome to the site, " . $name . "<br />";
echo "THEN WHO WAS NEW LINE?" . "<br />";
回答by Douwe Maan
HTML ignores all newlines, so you'll have to use <br />to insert a line break.
HTML 会忽略所有换行符,因此您必须使用<br />插入换行符。
回答by godzilla
if you nest string in pretags then /nwill work.
如果在pre标签中嵌套字符串,则/n将起作用。
echo "<pre>line one\nline two</pre>";
but font will not be same as non pre'ed text
但字体不会与非预先编辑的文本相同
回答by bobince
HTML isn't plain text. When you want a line break, you have to insert it with markup, such as <br>... though probably separate paragraphs would be more appropriate.
HTML 不是纯文本。当您想要换行符时,您必须使用标记插入它,例如<br>...虽然可能单独的段落更合适。
Also since HTML isn't plain text, you need to HTML-escape it when you output it. It doesn't matter for “Sergio”, but it would matter if someone's name was “Brian <i> Smith”, the guy with the unusual middle name who would turn your whole page italic if you didn't escape it properly.
此外,由于 HTML 不是纯文本,因此在输出时需要对它进行 HTML 转义。这对“Sergio”没有影响,但如果某人的名字是“Brian <i> Smith”就很重要了,这个中间名不寻常的人,如果你没有正确地转义它,他会把你的整个页面都变成斜体。
<body>
<h1>It works!</h1>
<p>
So someone tells me your name is <?php echo htmlspecialchars($name); ?>.
</p>
<p>
Welcome to the site, <?php echo htmlspecialchars($name); ?>.
</p>
</body>
回答by Wally
If your not running it through a web browser you can echo or printf "\n"
如果您不是通过网络浏览器运行它,您可以 echo 或 printf "\n"
回答by Chris Crew
<html>
<Head>
<?php
$name = "Sergio";
?>
</Head>
<body>
<h1>It works!</h1>
<?php
echo "So someone tells me your name is " . $name . ". ";
echo "Welcome to the site, " . $name . "<br/>";
echo "THEN WHO WAS NEW LINE?";
?>
</body>
I would put a breakline instead will do what you are looking for
我会放一条断裂线而不是做你正在寻找的

