Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

what is the difference between `/` and `` in php

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...