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 Basic PHP Website (2018) Adding a Basic Form Working with Concatenation and Whitespace

Pai Boony
Pai Boony
Courses Plus Student 4,529 Points

problem with "\n"

my code is <?php $name = $_POST["name"]; $email = $_POST["email"]; $details = $_POST["details"];

echo "Name " . $name ."\n"; echo "Email " . $email ."\n"; echo "Details " . $details; ?>

in the browser still show: Name pai Email pai@hotmail.com Details aaa

but when I view page source it show line break. Name pai Email pai@hotmail.com Details aaa

Why is that? can anyone help!!

4 Answers

Courses Plus Student 11,316 Points

If you view the source, it should look correct. If you want it to render correctly in the browser, you would need to add some <br />'s, for example:

echo "Name " . $name . "<br />\n";
echo "Email " . $email . "<br />\n";
echo "Details " . $details . "<br />\n";

Hope that helps

Madhuri Charugundla
Madhuri Charugundla
2,383 Points

The newline character \n is not an HTML element. It can be used in PHP only. So when you display in browser it still shows up as one line text. You can use <br /> along with \n so that it will display correctly in both browser and in view page source.

Alena shows how to correct this in two different ways towards the end of the video. You can add the HTML <br /> tag as shown above, or use the second method by using <pre> to have the PHP line returns shown properly in the browser. Fun to discover this now, as I have been wondering why we have been double-entering line returns all along throughout these tutorials.

Alena corrects herself in the video. \n is a sequence of characters interpreted by php, not by html. To appear on a new line you can do it in one of two ways.

  1. Add <pre> open and close tags around the block of code, or
  2. Use <br> which does get interpreted by html