Hello:

I have a question regarding "\n" and break tag "<br />".

I would like to know what's the difference between the two. Could you explain the difference. Give me some live code demo. If you can recommend online resources on escape characters that would help me to understand better. Thanks!

a) I thought the newline eacape character will generate a newline and text follow it will automatically goes to the next line. Actually it is not.

My code example:

<?php

  echo "hello world \n\n";
  echo "this is a php script.";
?>

b) I also see "\n" is being used to crate a table in PHP code. what would happen if newline character is omitted?

while ($rows = mysql_fetch_assoc($result) )
  {
    echo "<tr>\n";
    foreach ($rows as $value)
    {
      echo "<td>\n";
      echo $value;
      echo "</td>\n";
    }

echo "</tr><br />"; //break into next row
  }
  echo "</table>\n";
 
    1. phpsj06 wrote:

      would like to know what's the difference between the two. Could you explain the difference.

      Certainly. An "\n" in PHP doesn't mean a literal backslash followed by an 'n' -- PHP interprets this sequence as a special character, the newline character (if you look at an ASCII table, this is decimal #10). "<br/>" is an HTML entity. It means absolutely nothing except when parsed by browsers. Browsers interpret this as a line break and display it as such. It's just like typing "<b>Hi</b>". Do you see that on your screen? Of course not; your browser parses the HTML, and displays "Hi".

    2. phpsj06 wrote:

      a) I thought the newline eacape character will generate a newline and text follow it will automatically goes to the next line. Actually it is not.

      Actually, it is. If you're looking at this code from a browser, you'll have to view the actual source of the page. You'll see that the "\n" sequence did in fact move the text down one line. Why doesn't it appear this way? Because literal line breaks in blocks of HTML code don't tell a browser looking for HTML code to interpret to go down one line. Now, THIS code would look as expected:

    <?php
    
      header('Content-Type: text/plain');
    
      echo "hello world \n\n";
      echo "this is a php script.";
    ?>

    Why? Now that we've told the browser you're just sending plain text, it's not interpreting the data generated as HTML code -- instead it just displays whatever you send. Therefore, if you send the "text/plain" header, echo'ing "<br/>" wouldn't create a new line since the browser isn't interpreting the data as HTML code.

    1. phpsj06 wrote:

      b) I also see "\n" is being used to crate a table in PHP code. what would happen if newline character is omitted?

      Nothing -- the reason people write code like that is to make the HTML code generated easier to read.

    2. phpsj06 wrote:
      echo "</tr><br />"; //break into next row

      I'm not sure why you have a "<br/>" here... it's definitely not necessary (and in fact, I believe it might make your table look very odd). Instead, I believe you're looking for a literal line break ("\n").

      Write a Reply...