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 Adding a Contact Form Working with Concatenation and Whitespace

Stavros Anastasiadis
PLUS
Stavros Anastasiadis
Courses Plus Student 7,984 Points

Break line "\n"

I am using the syntax as the video: echo "Name:" . $name . "\n";

but doesn't seems to works, it only works with "<br>"

am i missing something?

(plz delete the post... i had to view source page code)

1 Answer

Hi Starvos,

Hope you don't mind but I might just elaborate on your question with the answer you found so that other folks who might be coming across the same issue and learn from it

In this course video, when the form is submitted, it takes you to a page that spits out the input entered into the form.

Now, even though Starvos had correctly used the line breaks above like so...

echo "Name:" . $name . "\n";

...it wasn't producing the required line breaks for format.

In fact, it was just spitting it out all in one line, no spaces or breaks. Like so...

Name:RandyEmail: test@email.comMessage: This contains my message.

In the video, and as Starvos said, the only way to see the format WITH the line breaks and any other white space was to right click the page in your browser window and select "View Source"

Then, it would display the format correctly like so....

Name: Randy
Email: test@email.com
Message: This contains my message

As the video demonstrates, if you want to render the input into a proper format with the line breaks and spaces, you can wrap pre tags around it... these guys here...

<pre>
    .......code goes here
</pre>

The beauty with pre tags is that it maintains the format. To give you an example if I was to go....

<pre>
       Higher still and higher
         From the earth thou springest
       Like a cloud of fire;
         The blue deep thou wingest,
And singing still dost soar, and soaring ever singest
</pre>

Then when it gets rendered the output would keep the same format, with all the line breaks and spaces just like I typed out above...

       Higher still and higher
         From the earth thou springest
       Like a cloud of fire;
         The blue deep thou wingest,
And singing still dost soar, and soaring ever singest

So the pre tags can be pretty handy for that reason.

So if we go back to our example in this video tutorial, when you code in all your php and echo it out with line breaks, spaces and such, if you wrap it in pre tags, when you go to echo it out, instead of spitting it out in one unformatted single squished up line, it will render your line breaks and spaces just the way you had asked.

Hopefully this sheds some light on how to render your line break text and also teach anyone reading this a little about the ever so handy pre tags

Happy coding guys!

Travis Stewart
Travis Stewart
15,188 Points

Thanks for this detailed explanation.