php – Difference between PATH_SEPARATOR and DIRECTORY_SEPARATOR

Question:

The documentation on this is quite vague, what little there is does not clearly clarify the difference between the usage and purpose of the following two PHP constants:

PATH_SEPARATOR

e

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 the / or \ .

But the fact that the two constants exist tells me right away that this will not be true, two things would not have been created for the exact same purpose!

Reading what is present in the manual in PHP: Predefined constants – Manual , I see:

Predefined constants

The constants below are defined by this extension and will only be available when the extension has been compiled with PHP or dynamically loaded at runtime.

DIRECTORY_SEPARATOR (string)

PATH_SEPARATOR (string)

That is, I don't see anything, not even a small description of the constant is present.

If you see in PHP: Predefined Constants – Manual (English) , say in ; and , !?

Question

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

Answer:

PATH_SEPARATOR

It is a character used to separate directories, into 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 supports / in some cases.

Scroll to Top