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 Build a Simple PHP Application Creating the Menu and Footer Including the Header

Boris Vukobrat
Boris Vukobrat
4,248 Points

Breaking up html elements?

So we broke the original html element

<div id="content">
...
</div>

into two included files header.php and footer.php. Here is the post about it.

It can't be more DRY, but it seems to me at the high price - much less readability. In the complex php project, we could have many includes so breaking up tags around the files could really make debugging like a hell. I think that is called spaghetti code, isn't it?

1 Answer

Hugo Paz
Hugo Paz
15,622 Points

Quite the contrary. By putting everything in neat "boxes" - header, footer, etc not only you avoid repeating code (dry) but it actually makes it easier to find errors. If you have an error in a page and all other pages are working right, you know the problem is not in the included files since they are working everywhere else.

The opening and closing tags might get confusing at first but if you comment your code you can avoid confusions.

Here's a great thread on spaghetti php

Boris Vukobrat
Boris Vukobrat
4,248 Points

It is not the question regarding DRY, and having header and footer in separate files.

I was referring to go a step beyond that and breaking html tags into different files. We broke up a div tag - opening into header file, and closing tag into footer file. I might use header.php in dozen of pages, and don't have

<div id="content">

(the last line of header.php) in all of them. That is the middle section of the page, and could be quite different among all the pages. So why would I confusing myself with including a tag from the middle section into the header?

Well, of course we could use that broken tag in every page, it doesn't disturb anything. I am just saying it is not necessary, and could be slightly complicating things.

Boris Vukobrat
Boris Vukobrat
4,248 Points

I just noticed there are a lot of discussions regarding this 'issue' in the following lesson.

Well I think it wouldn't be much confusing after a few php projects under fingertips : )). Since I was working with JSP, this isn't any worse (on contrary).