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 Conditionals & Loops Foreach Loops

Nick Woodland
Nick Woodland
13,870 Points

Why is the <li> tag kept outside the php tags?

It seems odd having that lone closing curly brace inside it's own php tag.

2 Answers

<li> is an html tag, if you keep it within php code php will not know what it is and will not execute

what you are essentially doing is telling the php parser to stop parsing after the opening curly brace, and start again right before the closing curly brace. it doesnt bother with the stuff in the middle and sends it to the browser as is, ie. html code

an alternative you can do is echo the html as string text, so that php will send it as text to the browser, and then the browser can parse it as html. eg.

echo "<ul>"; echo "<li>list item 1</li>"; echo "<li>list item 2</li>";

Nick Woodland
Nick Woodland
13,870 Points

Thanks for that, I was incorrectly assuming that the php interpreter would understand the html tags.

Konrad Pilch
Konrad Pilch
2,435 Points

They say not to mess PHP with HTML code and keep it separately because if e.g something goes wrong or whatever , if i remember well, its still good or something : p if im correct

Konrad Pilch
Konrad Pilch
2,435 Points

Oh well, its over 9 months xd, how are you getting on? :D did you made a social website or something ? :D

Yes it seems odd but you could write it this way:

<?php foreach($social_icons as $icon) { echo "<li> ..... </li>"; } ?>