Learn how to not break things on other people’s OS’s. \o/
DIRECTORY_SEPARATOR is a predefined constant provided by PHP. Windows uses \
and Unix (Linux/MacOS) use /
.
Windows is generous though. If you send it a path like \my\awesome/ridiculous/path
, it won’t break on you. For that reason, and because in WordPress we’re always using plugin_dir_path() and get_template_directory(), I went years before discovering the DIRECTORY_SEPARATOR. I just always used /regular/directories/like/everyone/else
.
But what happens if the OS sends you a path? Windows will send you \my\awesome\path
and then you might try to split it with:
$parts = explode( '/', $that_path_from_the_OS );
So remember to use DIRECTORY_SEPARATOR when you’re receiving paths:
$parts = explode( DIRECTORY_SEPARATOR, $that_path_from_the_OS );