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 Daily Exercise Program Combining Strings

Claudio Vendrame
Claudio Vendrame
4,964 Points

Back slash n doesn't work for me.

Back slash "\n" doesn't have any effect as a line break for me. Another difference is that single quote and double quote have different results: For me double quotes show the inside of string and single quotes doesn't .

Is it my local server php7 version problem?

I get around with </br>

Simon Coates
Simon Coates
28,694 Points

single quotes support fewer escape sequences, and don't support variable binding. A \n and a <br> are different. \n is a new line in the source code, not the presentation.

Claudio Vendrame
Claudio Vendrame
4,964 Points

Thank you for the reply. I can't get the same results as in the video. This is not from the video but I still can not see the problem: // Test one echo '1- Hello there\n '; echo "2- Hello there\n "; echo "3- Hello there " . "\n"; echo "</br>"; // Test two $string_one = "Hello $name \n"; echo $string_one; $string_one = "Hello $name " . "\n"; echo $string_one; echo "</br>";

There is no new line for all examples, besides the <br>

1 Answer

Simon Coates
Simon Coates
28,694 Points

Using

<?php

 // Test one 
echo '1- Hello there\n '; //should be literal rather than newline.
echo "2- Hello there\n "; // should work
echo "3- Hello there " . "\n";
echo "</br>"; //only interpreted as new line in presentation.
// Test two
$name= "somename";
$string_one = "Hello $name \n";
echo $string_one;
$string_one = "Hello $name " . "\n"; 
echo $string_one; 
echo "</br>";

I ran this from the console (not via the browser) and got:

>treehouse:~/workspace$ php file.php                                                                                                 
1- Hello there\n 2- Hello there                                                                                                         
 3- Hello there                                                                                                                         
</br>Hello somename                                                                                                                     
Hello somename                                                                                                                          
</br>treehouse:~/workspace$                                                                                                             

The new lines all show up in the source code, but the br tags will show up when looking via the normal browser view. Within a browser view \n is treated as 'white space' and ignored. To see the difference, view your page via the browser, then right click and select "view source" to get the code view.

Simon Coates
Simon Coates
28,694 Points

issue is also discussed at http://stackoverflow.com/questions/24026250/the-difference-between-n-and-br-in-php : "<br /> will make a new line when you view the page as rendered HTML, whereas \n will make a new line when you view the source code.". It also discusses PHP_EOL and the need to use double quotes to get the new line.