PHP lesson learnt.. always use the DIRECTORY_SEPARATOR constant

Always use the DIRECTORY_SEPARATOR constant when pointing to files/directories on the server-side. It will make your system more portable.

// Do you usually do this?
require_once dirname( __FILE__ ) . '/bootstrap.php';

// Or this? Don't!
require_once dirname( __FILE__ ) . '\bootstrap.php';

// Do this:
require_once dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'bootstrap.php';

Windows boxes uses backslash as a directory separator while for example Unix uses forward slash. Not using the DIRECTORY_SEPARATOR constant may lead to page-halting errors when for example require() fails locating your file.

Heads up: don’t use it in web URLs 🙂

This entry was posted in PHP and tagged , , . Bookmark the permalink.