PHP 换行 \n 和 \r\n 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10331855/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:56:00  来源:igfitidea点击:

PHP new line \n and \r\n not working

phplinenewlinebreak

提问by Dan1676

  $rows = mysql_num_rows($result) ;
  for ($j=0 ; $j < 3 ; $j++) {
  for ($i=0 ; $i < 3 ; $i++) {
    $row = mysql_fetch_array($result) ;
    echo '<a href="image2.php?id='.$row['ID'].'">'."<img src='".$row['Image']."' />".'</a>' ;

  }
   echo "\r\n";
  }

The code displays three groups of three images. My understanding was that \r\n and \n (double quotes) should create a new line. However it is just inserting a space between the images. Is the way am callign \r\n wrong or is it am using the wrong code to isneert a new line (line break)

该代码显示三组图像,每组三张。我的理解是 \r\n 和 \n (双引号)应该创建一个新行。然而,它只是在图像之间插入一个空格。我呼号\r\n的方式是错误的还是使用错误的代码来换行(换行)

Examples (# = one image):

示例(# = 一张图片):

Without echo \r\n: ######### With echo \r\n: ### ### ###

没有回声 \r\n:######### 有回声 \r\n:### ### ###

采纳答案by Nadh

Your echo "\r\n";is outside the loop. Move it inside the loop.

echo "\r\n";在循环之外。将其移动到循环内。

Also, if you want the line breaks to be visible in the browser, you should print a <br />too.

此外,如果您希望换行符在浏览器中可见,您也应该打印一个<br />

  $rows = mysql_num_rows($result) ;
  for ($j=0 ; $j < 3 ; $j++) {
  for ($i=0 ; $i < 3 ; $i++) {
    $row = mysql_fetch_array($result) ;
    echo '<a href="image2.php?id='.$row['ID'].'">'."<img src='".$row['Image']."' />".'</a>' ;
  }
    echo "<br />\n";    
  }

回答by Jon

Whitespace is not displayed verbatimwhen it's part of HTML text. \rand \nare not universal constants; they are just characters, and it's up to whatever program consumes them to decide what to do with them.

当空白HTML 文本的一部分时,它不会逐字显示\r并且\n不是普遍常数;它们只是字符,由使用它们的任何程序来决定如何处理它们。

You should use <br>instead.

你应该<br>改用。

回答by codaddict

You need:

你需要:

echo '<br />';

instead of:

代替:

echo "\r\n";