Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Listing Inventory Items Introducing Arrays

the intution between ln and <br> in PHP

Hello!

I can figure out why in the first code I need to use a <br> for each item to appear on its separate line, meanwhile for the second code, I can use \n and each line shows on its own line. How do you know when \n will cause your a line break on the browser. I know that it works mainly in view page sourse, but..

<?php
if($_SERVER['REQUEST_METHOD']=="POST"){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    $email_body = "";
    $email_body = $email_body."Name : ".$name."<br>";
    $email_body = $email_body."Email : ".$email."<br>";
    $email_body = $email_body."Message: ".$message."<br>";

    //echo $email_body;
     //echo $_SERVER["REQUEST_METHOD"];
    header('Location:contact.php?status=thanks');
    exit;
}
?>

2nd code

<?php

$flavors = array ("Vanilla", "Chocolate", "Strawberry");





?>
<pre>
<?php

 echo $flavors[0]."\n";
 echo $flavors[1]."\n";
 echo $flavors[2]."\n";


?>
</pre>

3 Answers

The br tag is an HTML tag and so it would only ever function in a browser or similar application. \n is a newline character in your code. So \n would normally only be used when you want your code to be formatted in a specific way, or it can also be a new-line if you are messing with plaintext.

Hello Christian,

By looking at the 2 code at the top, how would you know which to use, so that on the browser you see a new line when it is required.

thanks!

If you are going to print this on a browser use br. If you have to use the newline character, then you would have to make something in php which adds a br tag whenever a newline character is encountered in the string. There are probably many approaches to this, but this is one of them.

What's important to understand is that you have 2 languages here that are very very different (1 isn't even a programming language!), and so printing stuff on separate rows can be achieved in ways that differ from one language to the other.

Another way to view this by comparing what each command does in each language. \n is basically the same as pressing "enter" while typing your code. br on the other hand is a MARKUP element. Markup, as you probably know, is a tag that describes how elements should be placed and styled on a page; so if you have a paragraph tag (p tag) even if you echo out \n HTML will only interpret this as a character (just like it would interpret a or 1 or @). It wouldn't go to the next line because \n isn't a markup code that describes such a layout - br on the otherhand, is.

As I explained above, \n has the same effect as hitting your "enter" key in the code. So if you had this code:

<p>This is <?php echo "\n"; ?> a paragraph.</p>

On your browser all the text would appear in one line, however if you check the source code as your browser sees it (Ctrl + U) you will see that the source CODE is like this:

<p>This is
 a paragraph.</p>

That's your newline character right there! But you will only see it in your source code, and not in your page because your browser will ignore the newline for the same reason it ignores tabs, extra spaces (2 or more spaces the one after the other), and so on.

Hope this helps! :)

Hello Christian,

That is exactly what I have come to understand, but why does the code below appear in the browser with a new line after each item in the array is printed to the screen:

Please run this code and you will see what I mean:

<?php

$flavors = array ("Vanilla", "Chocolate", "Strawberry");





?>

<?php

 echo $flavors[0]."\n";
 echo $flavors[1]."\n";
 echo $flavors[2]."\n";


?>

~~

thanks!

I just ran your code and the browser gave the following output:

Vanilla Chocolate Strawberry

all in one line. If I view the source code, I get this:

Vanilla

Chocolate

Strawberry

all on separate lines as I expected. Are you getting different results?

Oh, I am running my code via Firefox, and on my browswer each flavor is on a seperate line. Well, at least now I know what it should be in theory, so this could be a browser issue. Sorry for the back and forth Christian. Thank you very much!

No worries, I'm here to help! :)