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

Chris Collier
Chris Collier
17,774 Points

Is the use of PHP include for templating common/best practice?

I'm a little bit into the beginning PHP section of the PHP track. I was really amazed at the ease of using an include to template a site's header, footer, and other common elements. However, I'm hesitant to file this away as a common tool. I was wondering specifically about:

  1. Does the use of PHP includes cause any issues with SEO (specifically, are crawlers able to index the page in HTML as served by the server after PHP execution)?
  2. Maybe I'll see more of this now that I know to look for it, but I don't think I see a lot of websites where every page is .PHP instead of .HTML. This leads me to believe that there are other (possibly better) ways of handling templating. I know that tools such as Dreamweaver and MS Expression have their own ways of handling templates, but they seem proprietary and I would think it would be better for portability to use something standardized like an include.

Thanks!

3 Answers

Chris Bell
Chris Bell
9,125 Points

Hey Christopher,

Just to follow up on Miguel's comments, rewrite rules are generally used generate the URL in most websites now, if you take a look at most WordPress websites there all PHP but normally the URL's end with a "/".

With regards to the includes, this is most defiantly best practice for your use case. If you have any components to your page that you need more that once be sure to use an include to prevent duplicate code. You will find that most websites contain multiple includes all put together to create a page.

Once you feel comfortable with the basics of PHP I would highly recommend taking a look into Laravel. It takes all the hardwork out of programming with PHP and leaves you to just do the fun stuff.

Once PHP processes the file it generates HTML and there are no issues with SEO. To a crawler it makes no difference how the HTML page is generated, whether by PHP, ASP, or written in your favorite text editor. The purpose of using PHP templates and includes in this example is to reuse components that are the building blocks for pages.

Lastly, don't be confused with file extensions like .PHP or .HTML. A server can advertise a .HTML file that was generated via PHP.