5

I am trying to understand examples in PSR-0, but to no avail. I know that \ is directory separator (at least in my windows OS), and in my humble opinion, it can't be equivalent to /. I googled the difference between them and found no result.

Adib Aroui
  • 4,981
  • 5
  • 42
  • 94
  • 1
    Fortunately, it's not an opinion question so your opinion doesn't change things. Yes, Windows uses the backslash "\" where other systems use "/" and yes PHP does not need special handling for that most of the time when making file paths. You can just use a normal forward slash even on Windows. – Elin Aug 16 '15 at 13:52
  • Actually "\" and "/" serve different purposes throughout PHP... it all just depends upon what you're doing. – l'L'l Aug 16 '15 at 13:54
  • The backslash in paths is a heritage from MS-DOS 2.0 (version 1.0 didn't have directories at all). It was chosen over traditional `/` forward slashes to obfuscate where the idea was borought from. However standard `/` directory separators also worked on DOS/Windows ever since. – mario Aug 16 '15 at 14:06

4 Answers4

13

When running under Linux or MacOS, PHP only allows / as a directory separator.

When running under Windows, PHP accepts either / or \ as a directory separator; it treats them exactly the same.

In virtually all cases, it is better to always use /, because that will allow your code to run on any platform. If you use \ for directory separators, then your code will only work on Windows.

The difference on Windows is to allow compatibility with other software that might provide paths with \ separators, but unless you specifically need to do that, it's best to stick with /.

Also, there is another use of \ in PHP which might be confusing you (particularly as you mentioned PSR0). The \ is also the separator for PHP namespaces.

Namespaces are not the same as directory separators, but they can end up looking like them, because common practice is to organise a project such that the namespaces match the directory structure. This is done to make your code modules easy to find and easy to write an autoloader for them, and it is thus the recommended way of structuring a project as per PSR0, but it is not compulsory in the PHP language; namespaces are not the same as directory paths.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • 1
    A backslash also has a special meaning inside strings, e.g. newlines are `\n`, and in regular expressions, e.g. `\s` is a whitespace character. To use paths with backslashes in literal strings, you need a good understanding of how PHP interprets string literals. Using a forward slash saves you the headache. – alexis Aug 16 '15 at 14:01
3

The term "backslash" is on of the most incorrectly used terms in computing. People often refer to forward slashes as backslashes, especially when referring to URLs. Web addresses (URLs), such as http://foo.bar/dash/, contain forward slashes, rather than backslashes. The difference between a backslash and a forward slash is defined below:

Backslash: \

Forward Slash: /

A good way to remember the difference between a backslash and a forward slash is that a backslash leans backwards ( \ ), while a forward slash leans forward ( / ).

In Windows backslashes are used to separate directories in file paths (ex:C:\Program Files\Common Files\microsoft shared). On based Unix systems, forward slashes are used for the same purpose (ex:/System/Library/Screen Savers).

Forward slashes can also be called simply "slashes," since they are much more commonly used than backslashes. (Slashes are also used as division symbols and in place of the word "or.") Therefore, the URL http://foo.bar/dash could be verbalized, "foo dot bar slash dash." If you say "backslash" when sharing a URL, people will know what you mean, but you might come across as a noob. Therefore, it's best to get in the habit of using the correct term.

  • yeah good way to differentiate between them. thank you for all the info. I am adding this to the `namespace` concept since it was the one confusing me +1 – Adib Aroui Aug 16 '15 at 14:43
2

\ is used for namespacing whereas / is a path separator on windows filesystem eg C:/Users/Sam/Documents

What the PSR class loaders allow you to do is, to find and load classes from directories using the information you provide in the namespace.

For example, a class called response.php can be placed inside folder samayo/http in the root directory of your projects and the full path would be samayo/http/response.php now, PSR allows you to use a namespace samayo\http\reponse to load the file using DIRECTORY_SEPARATOR by changing the directory separators and a little more behind the scene

samayo
  • 16,163
  • 12
  • 91
  • 106
1

\ is used as a delimiter for namespaces and / is used as the directory separator for files. The example is based on a unix system.

Although \ is used as a directory separator on you windows using / will work as well (e.g. for loading files). But if you would like the correct directory separator on all systems you can always use the system specific constant DIRECTORY_SEPARATOR.

flec
  • 2,891
  • 1
  • 22
  • 30