Robert
Robert

Reputation: 10390

directory_seperator vs path_seperator when and how to use

I was wondering when would one use php's PATH_SEPERATOR in code. I know it prints out a colon.

Why are there two constants, DIRECTORY_SEPERATOR and PATH_SEPERATOR, which have names that are synonymous?

I know that PATH_SEPERATOR prints out a slash.

When I think of either a path separator or a directory separator the thing that comes to mind is /.

Example of DIRECTORY_SEPARATOR use:

include 'files' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'image.jpg';

PHP's documentation doesn't really say much.

Upvotes: 0

Views: 650

Answers (4)

David Elson
David Elson

Reputation: 221

This is not just php. In general, directory separators and path separators are two different things.

A directory separator is withing one directory. For example, on unix, a/b/c has a directory separator or '/'. On windows, it would (probably :-) be a\b\c with a separator of '\\' (extra backslash for escape :-).

A path separator delineates multiple directories ... for example, consider the following search path:

/bin:/usr/bin:/home/me/bin

Within this search path containing 3 directories. Each of the 3 dirs uses '/' as its DIRECTORY_SEPARATOR, but the search path as a whole uses a PATH_SEPARATOR of ':' to delimit the constituent directories.

It means, first look in /bin, then if not there look in /usr/bin, then if not there look in /home/me/bin.

Hope this helps.

Upvotes: 0

user7000326
user7000326

Reputation:

A DIRECTORY_SEPERATOR separates directories (/ or \) e.g.

/home/your_user_name

A PATH_SEPERATOR separates paths (: or ;) e.g.

/home/your_user_name:/var/www/web0

Note that you don't have to use DIRECTORY_SEPERATOR as long as you stick to /. Windows and linux both understand this.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798686

DIRECTORY_SEPARATOR is used to separate elements of a single path. PATH_SEPARATOR is used to separate multiple paths as a single value, e.g. in include_path.

Upvotes: 0

AlexL
AlexL

Reputation: 1707

Although one might think they have synonymous names their job is actually quite different.

You use DIRECTORY_SEPARATOR when you build a path to a folder or file.

You use PATH_SEPARATOR when you're building the string for a collection of path and the PATH_SEPARATOR is used to separate individual paths in that collection.

Example:

$location1 = "include_folder".DIRECTORY_SEPARATOR."subfolder1";
$location2 = "include_folder".DIRECTORY_SEPARATOR."subfolder2";
$path_collection = $location1.PATH_SEPARATOR.$location2;

Upvotes: 1

Related Questions