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 PHP Basics (Retired) PHP Datatypes Strings

PHP new line

As Hampton shows in this video that "\n" makes a new line in php but it is not working on my PC.As far i understand \n creates a newline in the source code.So what is the best way to make a new line in php?

Sean Do
Sean Do
11,933 Points

Is your local server on your PC running? PHP can't run standalone like HTML/CSS/JS.

yes.. local server is running on my PC

Sean Do
Sean Do
11,933 Points

Can you copy & paste source?

<?php

echo "<h1>Hello php </h1>";

$greeting = "Hello Everyone!\n";

$greeting_ask = "How are you?";

echo $greeting; echo $greeting_ask;

?>

Try using \r\n instead of \n

3 Answers

Mike Costa
PLUS
Mike Costa
Courses Plus Student 26,362 Points

\n does make a new line but if you're echoing your output to a website, you should use a break tag <br> instead.

Try putting a space before the \n.

I had the same problem and learned to use PHP's newline to break function.

Example:

echo nl2br("foo isn't\n bar");

See the site http://php.net/manual/en/function.nl2br.php

Also, the \n will work correctly within the confines of the HTML "pre" tags.