Difference between PATH_SEPARATOR and DIRECTORY_SEPARATOR

24

The documentation on this is rather vague, the little that exists does not clearly clarify the difference between the use and purpose of the following two PHP constants:

PATH_SEPARATOR

and

DIRECTORY_SEPARATOR

At first glance, it seems that both give us the same practical application, that is, detect the path separator in the operating system, for example / or \ .

But the fact that there are both constants tells me soon that this will not be true, two things would not have been created for the same exact effect!

Reading what is present in the manual in PHP: Predefined Constants - Manual , I see:

  

Predefined Constants

     

The counters below are defined by this extension and will only be   available when the extension was compiled with PHP or loaded   dynamically during execution.

     

DIRECTORY_SEPARATOR (string)

     

PATH_SEPARATOR (string)

That is, I see nothing, not even a small description of the constant is present.

If you are going to see PHP: Predefined Constants - Manual (English) , it speaks in ; and , !?

Question

What is the difference between the two constants and their practical use for each?

    
asked by anonymous 22.01.2014 / 12:19

3 answers

20

PATH_SEPARATOR

Is a character used to separate directories, in a single string, as you can see in include_path " in the php.ini file.

  • UNIX

    The value is :

      

    / var / www / a: / var / www / b

  • Windows

    The value is ;

      

    c: \ directory \ a; c: \ directory \ b

DIRECTORY_SEPARATOR

In this case, it is related to the / separator in UNIX and \ in Windows.

Note: Windows also accepts / in some cases.

    
22.01.2014 / 12:32
11

DIRECTORY_SEPARATOR is the separator used to traverse directories. We are accustomed to \ in Windows or / in * nix.

PATH_SEPARATOR is the separator used to group more directories into environment variables (usually only in PATH ). In windows we have ; and in * nixes have : .

    
22.01.2014 / 12:34
3

PATH_SEPARATOR This constant is used to separate paths with the correct character according to the operating system that in windows are separated by ; and in linux by :

DIRECTORY_SEPARATOR , it is used to place the separator character of directory that in windows is \ and in linux /

The two constants avoid specifying directories / paths directly (hardcode), which makes the code portable.

    
22.01.2014 / 12:42