35

Isn't this / ?

Why is there a constant for it? It's not like it can change, right?

Alex
  • 66,732
  • 177
  • 439
  • 641

3 Answers3

98

PATH_SEPARATOR is the character used to separate many paths in a unique string (like include_path in php.ini).

Its value is ':' on a UNIX system and ';' on a Windows system.

What you're talking about ('/' on UNIX and '\' on Windows) is the DIRECTORY_SEPARATOR constant.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
AlterPHP
  • 12,667
  • 5
  • 49
  • 54
13

As your original question states: "Why is there a PATH_SEPARATOR constant?", windows uses a semi-colon ;, while other systems use a colon :

However I think you've mistaken PATH_SEPARATOR with DIRECTORY_SEPARATOR

PATH_SEPARATOR delimits multiple paths in the same string. For example when used in windows environment variables.

c:\path\to\a;c:\path\to\b

DIRECTORY_SEPARATOR separates the directories within the path: In Windows

\

In other systems

/

As mentioned by others, windows also accepts /

tvanc
  • 3,807
  • 3
  • 25
  • 40
Ben Rowe
  • 28,406
  • 6
  • 55
  • 75
  • Open up a command console and try `cd /valid/path/` in windows. I think you'll find that this works – Ben Rowe Jan 19 '13 at 22:20
  • Try `cd /`, and you'll see that it doesn't. – Karoly Horvath Jan 20 '13 at 10:36
  • @KarolyHorvath, I just did on 3 windows 7 machines. Worked without any problems. – Ben Rowe Jan 21 '13 at 22:09
  • what's the point of trying *three* machines with the *same* version? Try a different version... – Karoly Horvath Jan 22 '13 at 09:38
  • @KarolyHorvath: Windows does support a mixture of forward slashes and backslashes, at the API level. See, for example: http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858%28v=vs.85%29.aspx "lpFileName [in]: The name of the file or device to be created or opened. You may use either forward slashes (/) or backslashes (\\) in this name." – jmc Feb 11 '13 at 15:24
  • 1
    That's exactly the problem - it supports it at the API level. Try `system("dir /mydirectory")` - it will complain, because it thinks that's a parameter to the `dir` command. So although PHP supports `/`, you can easily end up with weird problems because you assumed it's going to work *everywhere*. I just wanted to point out this. – Karoly Horvath Feb 11 '13 at 15:47
4

It can. It is \ in Windows and / in Linux (and prettymuch everywhere else), although modern versions of Windows do accept / as a separator.

Ooops this is about the DIRECTORY_SEPARATOR constant.

PATH_SEPARATOR is indeed the constant to separate various paths as seen in PéCés answer.

Community
  • 1
  • 1
GolezTrol
  • 114,394
  • 18
  • 182
  • 210