简体   繁体   English

PHP代码中的nl2br不会生成新行

[英]nl2br in php code does not generate new line

I'm trying to print two 3-digit random numbers one below the other. 我正在尝试将两个3位随机数打印在另一个下方。 I've tried a bunch of functions but it does not seem to output a new line and the second random number. 我已经尝试了很多函数,但似乎没有输出新行和第二个随机数。

Code is as follows: 代码如下:

<?php
echo "<h1><center>Random php</center></h1>"; 
echo rand(100,990);
echo nl2br(“\n”,false);
echo rand(100,990);
?>

The output is as follows on the browser. 在浏览器上的输出如下。

Random php 随机PHP

698 Fatal error: Undefined constant '“\\nâ€' in /Volumes/Macintosh Media/code/random/main.php on line 4 698错误:第4行的/ Volumes / Macintosh Media / code / random / main.php中未定义的常量'\\\\ n'

You are using curly quotes instead of the ASCII double-quote character in nl2br : 您使用的是弯引号而不是nl2br中的ASCII双引号字符:

echo nl2br(“\n”,false);

instead of 代替

echo nl2br("\n",false);

PHP does not know what to do with the curly quotes and is trying to parse the entire thing ( “\\n” ) as the name of a constant (which of course does not exist). PHP不知道如何处理弯引号,并试图将整个内容( “\\n” )解析为常量的名称(当然不存在)。

In any case, it is much simpler to do as Harshit Shrivastava suggests and print a <br> rather than use the nl2br() function here. 无论如何,按照Harshit Shrivastava的建议进行操作并打印<br>而不是在此处使用nl2br()函数要容易得多。

nl2br insert line breaks where newlines (\\n) or carriage (\\r) occur in the string: nl2br插入换行符在字符串中出现换行符(\\ n)或回车符(\\ r)的地方:

<?php
echo nl2br("One line.\nAnother line.");
?>

This will output as folows 这将作为以下输出

One line. 一条线。
Another line. 另一条线。

For your code to work, use <br> 为了使代码正常工作,请使用<br>

<?php
echo "<h1><center>Random php</center></h1>"; 
echo rand(100,990);
echo "<br>";
echo rand(100,990);
?>

Check the link for more on this 检查链接了解更多信息

You should use <BR> . 您应该使用<BR>

<?php
echo "<h1><center>Random php</center></h1>"; 
echo rand(100,990);
echo "<BR>";
echo rand(100,990);
?>

nl2br is use to convert \\n to <BR> nl2br用于将\\n转换为<BR>

Look at your quotation marks. 查看您的引号。 Use the proper ones (ASCII quotes). 使用适当的(ASCII引号)。 Apart from that just use <br /> 除此之外,只需使用<br />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM