Use full paths in includes and requires [TotD]

Posted by hts | January 16, 2008 .

Problem

If you specify relative paths when you include a file, then you are wasting precious time. Doing so makes PHP resolve the OS path - that is, find the file, which is located relative to the file which requests it. This takes some time and slows down (well, not drastically, but if every bit matters, then just “fix” this problem)

Solution

Use full paths in includes and requires, because it takes less time spent on resolving the OS paths.
Basically, just use the full url. Example:

<?php
require('http://yoursite.com/files/menu.php');
include(
'http://yoursite.com/resources/style.css.php');
//and so on and so forth
?>

Also, as an additional tip, it is a good programming habit to define the URL of your site in a global somewhere, or in some close manner, so that if you change the URL, you won’t have to modify dozens of files. Good luck

Leave a Comment

If you would like to make a comment, please fill out the form below.

Name (required)

Email (required)

Website

Comments