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

Alex Watts
Alex Watts
8,396 Points

Why use PHP include?

I am currently watching "Build a Basic PHP Website" by Alena Holligan. Is it good practise to use the PHP include within a project. This method is confusing and it looks messy. Is there a better way of doing the same process?

3 Answers

Richard Hall
Richard Hall
9,139 Points

What is it that looks confusing? Are you asking why use PHP include vs PHP require? Or just generally asking why would it be useful?

If the latter, one common idea is to separate the header and footer from a page. DRY programming - that is, don't repeat yourself. Instead of re-typing a header and footer on every page, you could make a header.php and a footer.php and

<? include 'footer.php'; ?>

on each page and it will load it in place.

Hi, maybe require_once();

PHP.net require_once() page

Kevin D
Kevin D
8,646 Points

PHP include, require and include_once, require_once functions help us with code reuse. Sometimes we don't want to duplicate code so we can use these functions to copy code into the file.

It'll definitely be useful when you get to grips with it!