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 trialShafeeq Ahmed
6,058 Points\n
When I use the escape sequence "\n" inside paragraph tags (<p> </p>). It did not pull the content to next line. instead it moves the content to one space. but when I use it inside <pre> tags it moves the content to the next line. but Hampton taught in the video that it moves the following content to next line. Please explain.
10 Answers
Codin - Codesmite
8,600 PointsWhitespace is not displayed when it's part of HTML text.
\n is not a universal constant it is just a character, and it's up to whatever program/platform that consumes it to decide what to do with it.
You should use
or the self closing
```<br />```
(self closing version is XML friendly) when targeting a browser with your code.
The reason it works in Hampton's video is because he has it surrounded with
```<pre></pre>```
tags which display content as if it has come from a plain text file.
\n is not for use with web browsers, it is more commonly used for consoles and other platforms that support it as end of line.
Best practice is to use both \n and
```<br />```
at the same time so it displays correctly in both browsers and console, but really you only need to use what is required for your output, which in this case is just
```<br />```
Hope this helps solve the confusion.
```php
<section class="main">
<p>
<?php
$greetings = "Hello<br />";
$greetings{0} = "J";
$secondary_greeting = "How are you today<br />";
$thirdary_greeting = "Have a nice day!";
echo $greetings;
echo $secondary_greeting;
echo $thirdary_greeting;
?>
</p>
</section>
Shafeeq Ahmed
6,058 PointsIt worked Thanks
Gianmarco Mazzoran
22,076 PointsI find the error you must be include all with pre tag like this:
<pre>
<?php
$greetings = "Hello\n";
$greetings{0} = "J";
$secondary_greeting = "How are you today\n";
$thirdary_greeting = "Have a nice day!";
echo $greetings;
echo $secondary_greeting;
echo $thirdary_greeting;
?>
</pre>
Jason Anello
Courses Plus Student 94,610 Pointschanged from comment to answer
Jason Anello
Courses Plus Student 94,610 PointsShafeeq,
What Gianmarco posted is your problem. Hampton is still using the pre tags in the video but you switched over to p tags.
A \n
only creates a line break in the html source output. Not on the visual page. You have to use pre tags if you want to preserve the white-space in the html source. Without it, browsers will collapse excess white-space down to a single space.
I recommend trying this out in the browser if it's still not fully clear what's going on.
<?php
echo "<p>Hello World!</p>";
echo "\n";
echo "<p>Hello\nWorld!</p>";
echo "\n";
echo "<pre>Hello\nWorld!</pre>";
?>
I only put in echo "\n";
to make it easier to see what's happening in the source they are not part of the example.
View the rendered page and then compare with viewing the source of the page. You can right click on the page and then you should have the option to select "view source"
On the rendered page, The first 2 greetings will look exactly the same. All on one line. However, if you examine the page source you will see:
<p>Hello World!</p>
<p>Hello
World!</p>
<pre>Hello
World!</pre>
The second greeting is broken onto 2 lines because of that \n. The browser will collapse that into a single space because the p tag was used.
For the 3rd greeting, it appears on 2 lines because the line break in the source is preserved on the rendered page because the pre tag was used.
Gianmarco Mazzoran
22,076 PointsHi Shafeeq Ahmed, have you checked that you used double quotes like this " instead of single quotes '?
Shafeeq Ahmed
6,058 PointsGian, I used double quotes
Gianmarco Mazzoran
22,076 PointsCould you post your code so we can take a look at it?
Shafeeq Ahmed
6,058 PointsJason Anello, This video https://teamtreehouse.com/library/php-basics/php-datatypes/strings
Shafeeq Ahmed
6,058 PointsGianmarco Mazzoran,
Thanks for ur time and confederation
Here is my code
<?php
// This is my name
$name = "Eph ";
// $location = "Orlando, FL";
$full_name = "Mike The Frog";
$name = $full_name;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title><?php echo $name ?>| Treehouse Profile</title>
<link href="css/style.css" rel="stylesheet" />
</head>
<body>
<section class="sidebar text-center">
<div class="avatar">
<img src="img/avatar.png" alt="Mike The Frog" title="<?php echo $name ?>">
</div>
<h1><?php echo $name ?></h1>
<p><? echo $location ?></p>
<hr />
<p>Welcome to PHP Basics!</p>
<hr />
<ul class="social">
<li><a href=""><span class="icon twitter"></span></a></li>
</ul>
</section>
<section class="main">
<p><?php
$greetings = "Hello\n";
$greetings{0} = "J";
$secondary_greeting = "How are you today\n";
$thirdary_greeting = "Have a nice day!";
echo $greetings;
echo $secondary_greeting;
echo $thirdary_greeting;
?></p>
</section>
</body>
</html>
Gianmarco Mazzoran
22,076 PointsI find a typo on the first p tag you not open correctly the php tag! Try to see if this cause the error.
Shafeeq Ahmed
6,058 PointsGianmarco Mazzoran, can you please explain it little bit more
Gianmarco Mazzoran
22,076 PointsInside the body tag, you used a p tag to echo the variable $location but you forgot to open correctly the php tag inside of it. You have write
<p><? echo $location ?></p>
instead of
<p><?php echo $location ?></p>
Shafeeq Ahmed
6,058 PointsThank You Gianmarco Mazzoran
Gianmarco Mazzoran
22,076 PointsIt works?
Shafeeq Ahmed
6,058 PointsNo it doesn't
Shafeeq Ahmed
6,058 PointsThank u all Jason, Gianmarco , Ashley and all
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsHi Shafeeq,
Which video are you referring to?