PHP 5.6.0 released

Constantes pré-définies

Ces constantes sont définies par cette extension, et ne sont disponibles que si cette extension a été compilée avec PHP, ou bien chargée au moment de l'exécution.

DIRECTORY_SEPARATOR (chaîne de caractères)
PATH_SEPARATOR (chaîne de caractères)
Disponible depuis PHP 4.3.0. Un point-virgule sous Windows, deux points (:) sinon.
SCANDIR_SORT_ASCENDING (entier)
Disponible depuis PHP 5.4.0.
SCANDIR_SORT_DESCENDING (entier)
Disponible depuis PHP 5.4.0.
SCANDIR_SORT_NONE (entier)
Disponible depuis PHP 5.4.0.
add a note add a note

User Contributed Notes 3 notes

up
14
Anonymous
11 months ago
For my part I'll continue to use this constant because it seems more future safe and flexible, even if Windows installations currently convert the paths magically. Not that syntax aesthetics matter but I think it can be made to look attractive:

<?php
$path
= join(DIRECTORY_SEPARATOR, array('root', 'lib', 'file.php');
?>
up
4
Anonymous
5 months ago
In PHP 5.6 you can make a variadic function.

<?php
/**
* Builds a file path with the appropriate directory separator.
* @param string $segments,... unlimited number of path segments
* @return string Path
*/
function file_build_path(...$segments) {
    return
join(DIRECTORY_SEPARATOR, $segments);
}

file_build_path("home", "alice", "Documents", "example.txt");
?>

In earlier PHP versions you can use func_get_args.

<?php
function file_build_path() {
    return
join(DIRECTORY_SEPARATOR, func_get_args($segments));
}

file_build_path("home", "alice", "Documents", "example.txt");
?>
up
4
Paulo Marques
11 months ago
DIRECTORY_SEPARATOR is not necessarily needed, PHP always converts / to the appropriate character in its file functions.
It is good practice, though.
To Top