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

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?

<?php
            $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,

Cheers