Reputation: 1102
I have no idea if there's a technical term for this, so I didn't find anything on google nor this site.
A friend of mine who has been making sites for years, and actually set up a bussiness, uses a rather unique (to me) system.
He splits up his page into 3 parts, the header, body and footer. Puts them into 3 files, and then includes the header and footer in the body page, leaving only this: (example)
<?php include_once "Constants/Header.php"; ?>
<div id="Container">
<div id="Header">
HEADER
</div>
<div id="Menu">
<ul id="Nav">
<li>Menu item</li>
<li>Menu item</li>
<li>Menu item</li>
<li>Menu item</li>
<li>Menu item</li>
<li>Menu item</li>
</ul>
</div>
<div id="Body">
</div>
</div>
<?php include_once "Constants/Footer.php"; ?>
Is it good practise to code a site this way? -if so, why? And last but not least, do you code your pages this way?
Upvotes: 5
Views: 8153
Reputation: 671
It's not good but common practice. Slightly better would be 3 files, one for the header, one for the footer, and then one(typically: index.php) that includes the header, the footer and the content.
Code Redacted, see edit.
Edit 2024 Update:
Never pass unvalidated user data to require_once
! A better approach would be
$pages= ['page_home.php', 'page_about.php'];
require_once('header.php');
if (isset($_GET['file']) && in_array($_GET['file'], $pages)) {
$file = $_GET['file'];
require_once('pages/' . $file);
} else {
// Handle invalid input, e.g., display an error message
}
require_once('footer.php');
Upvotes: 2
Reputation: 112815
As several others have mentioned, splitting your code up into multiple PHP files can help you avoid duplicating your work unnecessarily. However, an even bigger benefit that can fall out of this is that the include
statement is the gateway to providing is implementing a sitewide architecture such as Model-View-Controller.
Using MVC, your code is split up not just in terms of parts of the page that the user sees (header, footer, body, etc), but also in terms of responsibility. At a high level, the "Model" manages the data and business logic, the "View" handles the user interface (including rendering data from the model), and the "Controller" handles requests and interacts with the appropriate parts of the Model.
I'm no expert on MVC by any means, but the benefits here are enormous. Each of the components can be tested individually. Designing the components to be "loosely coupled" helps you avoid repeating yourself and encourages writing reusable code.
There are a number of PHP web application frameworks that use MVC architecture, many of which are freely available (e.g., CodeIgniter).
Admittedly, MVC is not necessarily the "one true way" -- in your friend's case, implementing an architecture like this may be outside of the scope of what he wants to do. But in regards to your original question (in a roundabout sort of way I guess), the include
statement can (when used properly) be a very powerful and useful tool.
Upvotes: 5
Reputation: 14618
It's not good, neither bad.
Depends on what you want to do. If you want a simple-as-can-be limited set of webpages, it's good enough. Problems start appearing when you want a separate stylesheet for each page. Then some pages use a script, others include another script. In these cases, the headers will differ from page to page (they CAN be the same, but it's a bad practice to include files you're not using).
Then, if you are building some sort of interactions on the site, this just won't do. It's not a way to program. There is no room for actual PHP code. You can of course include PHP code, but it's an extremely bad practice to mix PHP and HTML in this way.
The best way you can make PHP work for you in this case is to use some sort of a framework or CMS. If you want to build a website with pages with little interaction (such as a website for a company, some presentations, news site, blog) - use a CMS, like Wordpress, Joomla, Drupal, DLE. If you need something more complicated, like a PHP-based web application of some sort, use MVC frameworks, go Zend framework, or something easier and smaller (here I am being extremely subjective) like CakePHP, CodeIgniter or Symfony.
Upvotes: 1
Reputation: 10219
Well, that's a pretty basic technic in PHP. It allows you to modify the header or the footer in one spot and being modified in every pages of the site using them.
It can be used for more purpose than that.
Upvotes: 1
Reputation: 310
I don't know about good practice, but if your site uses the same header and footer across all pages then it's a lot more efficient to include the code once rather than copy-and-pasting the same code into each page.
Future additions to your menu need to be added only the once and each page sees the change.
Upvotes: 2
Reputation: 55334
It is extremely helpful (for you as a developer and your successor) to use include
and split your PHP files. This way, if you need to change the footer, for example, you only have to change the one footer file, and the changes are reflected site-wide.
And yes, I always use include
.
Upvotes: 3