7

Say I'm defining a directory and then including files from it. Is it better practice to do:

define('PATH', 'C:/xampp/htdocs/includes/');
require PATH.'header.php;

or:

define('PATH', 'C:/xampp/htdocs/includes');
require PATH.'/header.php;
  • Note that you may replace "/" by DIRECTORY_SEPARATOR, a constant which will be replaced by "\" on Windows. It helps to be consistent. – Arseni Mourzenko Nov 23 '13 at 20:05
10

To avoid ambiguity, you should always add the trailing slash.

Not all files have extensions, so if you use this consistently in your codebase, you (and others) will be able to tell whether a path references a directory or a file at a glance.

|improve this answer|||||
  • 1
    Additionally, since extra slashes are ignored you won't accidentally attempt to open nonexistent files – whatsisname Nov 23 '13 at 20:11
  • 3
    This is always what I thought too until I realized that Directory.GetParent("C:\stuff\") will actually return C:\stuff instead of `C:` as you would expect – Joe Phillips May 5 '16 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy