Search code examples
phppath-separator

When to use DIRECTORY_SEPARATOR in PHP code?


require_once dirname(__FILE__).DIRECTORY_SEPARATOR . './../../../wp-config.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'inc/options.php';

The above code is from a plugin from the Wordpress. I don't understand why half of it uses DIRECTORY_SEPARATOR, but the other half uses "/" ?


Solution

  • Because in different OS there is different directory separator. In Windows it's \ in Linux it's /. DIRECTORY_SEPARATOR is constant with that OS directory separator. Use it every time in paths.

    In you code snippet we clearly see bad practice code. If framework/cms are widely used it doesn't mean that it's using best practice code.