Microsoft chose the backslash character ("\") as a directory separator, which looks similar to the slash character, though more modern version of Windows are slash-agnostic, allowing mixage of both types of slashes in a path.
If you prefer to hard-code the directory separator character, you should use the forward slash ( / ) character. It is the only recognized directory separator character on Unix systems, as the output from the example shows, and is the AltDirectorySeparatorChar on Windows.
In Windows you can use either \ or / as a directory separator.
As far as PHP is concerned, you might not need it when constructing a path, but it is important for anything you get from the OS.
From http://alanhogan.com/tips/php/directory-separator-not-necessary:
In attempting to write cross-platform, portable PHP code, I used PHP’s DIRECTORY_SEPARATOR constant to write path strings, e.g.
"..".DIRECTORY_SEPARATOR."foo"
, because the “proper” way to do it on Windows would be"..\foo"
while on everything else (Linux, UNIX, Mac OS X) it would be"../foo"
.Well, as Christian on php.net pointed out and the guys at Web Design Forums confirmed, that’s completely unnecessary. As long as you use the forward slash, “/”, you’ll be OK. Windows doesn’t mind it, and it’s best for *nix operating systems.
(Note that
DIRECTORY_SEPARATOR
is still useful for things likeexplode
-ing a path that the system gave you. Thanks to Shadowfiend for pointing this out.)
Mac OS Classic uses ":", for instance. See Wikipedia for details. Also it's considered good style avoiding 'magic numbers' or similar constructs.
Windows actually uses a backslash as the directory separator, although some environments that have Windows versions will translate between forward slashes and backslashes automatically (Python comes to mind).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With