PHP includes are a vital aspect of websites and should be incorporated into every Web Developer’s workflow.
Code via Chris Coyier and the CSS Tricks website
Main reason for using a PHP include:
- All code for a specific function is under 1 roof.
- If you need to make a change you change it in 1 spot instead of going to each and every individual page
When using and creating includes the files need to have the php extension on the file.
Index.html turns into index.php
Popular includes to create:
Meta data/ stylesheets/ head information
Separate website tracking code if you don’t/can’t include it in the footer.
General include statement:
<?php include(“header.php”); ?>
PHP include statement from root.
$path = $_SERVER[‘DOCUMENT_ROOT’];
$path .= “/common/header.php”;
DISCLAIMER: when using PHP includes you HAVE TO ensure ALL OF YOUR LINKS are sourced from the root.
- All internal navigation
- Direct links from 1 page to another that is not in the navigation
- Image file locations
Generally speaking this is something to get into the habit of doing anyway, that way if you move something you don’t have to remember to fix the link. Also I do believe that this is a general web standard.
Podcast: Play in new window | Download (Duration: 15:55 — 14.6MB)
Subscribe: Apple Podcasts | Android | Email | RSS