Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP PHP Basics (Retired) PHP Datatypes Strings

Bruno Dias
Bruno Dias
10,554 Points

\n doesn't break the line with no <pre>

If I remove the pre tag, the line doesn't break even with \n.

Is that correct?

            $greeting = "Hello, friends!\n";
            $greeting{0} = "J";
            $secondary_greeting = "How are you today?";
            echo $greeting;
            echo $secondary_greeting;

1 Answer

Hi Bruno,

If you're sending HTML to a browser, line breaks are added with <br>. Otherwise, if the output is going to a console or file, then use \n or PHP_EOL which inserts "The correct 'End Of Line' symbol for this platform."

The <pre> tag displays content as though it came from a file. That's why \n creates a line break in this situation. Here is an MDN article with more information on the <pre> tag.

I was trained to add both HTML and file line breaks, but in practice - for prototyping or learning, I take the lazy way out and use the line break appropriate for my intended output.

Hope this helps,