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

此生再无相见时 提交于 2019-11-30 23:45:16

问题


  $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)

Examples (# = one image):

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


回答1:


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

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

  $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";    
  }



回答2:


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

You should use <br> instead.




回答3:


You need:

echo '<br />';

instead of:

echo "\r\n";


来源:https://stackoverflow.com/questions/10331855/php-new-line-n-and-r-n-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!