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

Janos Antal
Janos Antal
1,451 Points

\n no new line in php 5.4?

Hello!

I just can not get a single new with any of the methods, please help:

<?php
    $greeting ="Hello, Friends\n";
    $secondary_greeting ="How are you today?\n";

    echo $greeting . "\n";
    echo $greeting{0} . "\n";
    $greeting{0} = J;
    echo $greeting . "\n";
    echo $secondary_greeting;
?>

Neither of this code part creates a new line. Everything is in one single line. Why is that?

cheers

2 Answers

Hi Janos,

I fixed the markdown on your code.

Since PHP is a server side language, it's output should be treated as HTML, because the browser is what renders it. To create line breaks in HTML, use a <br> tag, or wrap the output in <p> tags.

The new line character impacts formatting when viewing source code or when doing file operations. Also, when unsure which version of the new line character to use, php has a built-in constant called PHP_EOL that inserts the appropriate one.

<?php

// try this
$greeting ="Hello, Friends";
$secondary_greeting ="How are you today?";

echo $greeting . "<br>" . PHP_EOL;
echo $greeting{0} . "<br>" . PHP_EOL;
$greeting{0} = J;
echo $greeting . "<br>" . PHP_EOL;
echo $secondary_greeting . "<br>" . PHP_EOL;

?>

Hope this helps,

Cheers

Janos Antal
Janos Antal
1,451 Points

Cool! Thank you very much "<br>" works perfectly. Otherwise our lecturer uses \n in his lessons, it might need then correction? I might missed something somewhere but how could I do this black highlight of my code in the forum?

You're right, I don't think they ever show PHP_EOL, and that's OK. I just wanted to show you another way. Sticking with \n will be fine through the course.

If you're curious about how to add markdown like this on your own, checkout this thread on posting code to the forum . Also, there is a link at the bottom called Markdown Cheatsheet that gives a brief overview of how to add markdown to your posts.

Cheers!

lihaoquan
lihaoquan
12,045 Points

\n is used for Unix systems (including Linux, and OSX).

\r\n is mainly used on Windows.

\r is used on really old Macs.

So you should use the one that works on your OS. The instructor is probably using a MAC which works on OSX which explains why he uses \n

Janos Antal
Janos Antal
1,451 Points

to be honest one of those is working I tries \r, \r\n and also \n, still everything is in one line