Difference between PATH_SEPARATOR and DIRECTORY_SEPARATOR

Asked

Viewed 6,810 times

25

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, ie, detect the path separator in the operating system, for example the / or \.

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

Reading what is present in the PHP: Predefined constants - Manual, see:

Predefined constants

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

DIRECTORY_SEPARATOR (string)

PATH_SEPARATOR (string)

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

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

Question

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

3 answers

21


PATH_SEPARATOR

It is a character used to separate directories into a single string, as can be seen in include_path in the file php.ini.

  • 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 / on UNIX and \ on Windows.

Obs.: Windows also accepts the / in some cases.

  • 1

    In the linux shell type echo $PATH in the case of Windows type echo %path% - will see the difference

11

DIRECTORY_SEPARATOR is the tab used to go through directories. We are used to the \ on Windows or the / nos *Nix.

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

3

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

DIRECTORY_SEPARATOR, Serves to put the directory separator character that in windows is \ and on linux /

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.