©
本文档使用 PHP中文网手册 发布
下列常量由此扩展定义,且仅在此扩展编译入 PHP 或在运行时动态载入时可用。
DIRECTORY_SEPARATOR
( string )
PATH_SEPARATOR
( string )
Note:
PATH_SEPARATOR
是在 PHP 4.3.0-RC2 中引入。
[#1] orlov0562 at gmail dot com [2015-07-21 07:20:12]
While debugging, this function return error number and it's difficult to remember all errors codes, so I think it's should be there.
<?php
print_r([
'PREG_NO_ERROR' => PREG_NO_ERROR,
'PREG_INTERNAL_ERROR' => PREG_INTERNAL_ERROR,
'PREG_BACKTRACK_LIMIT_ERROR' => PREG_BACKTRACK_LIMIT_ERROR,
'PREG_RECURSION_LIMIT_ERROR' => PREG_RECURSION_LIMIT_ERROR,
'PREG_BAD_UTF8_ERROR' => PREG_BAD_UTF8_ERROR,
'PREG_BAD_UTF8_OFFSET_ERROR' => PREG_BAD_UTF8_OFFSET_ERROR,
]);
?>
Result:
Array
(
[PREG_NO_ERROR] => 0
[PREG_INTERNAL_ERROR] => 1
[PREG_BACKTRACK_LIMIT_ERROR] => 2
[PREG_RECURSION_LIMIT_ERROR] => 3
[PREG_BAD_UTF8_ERROR] => 4
[PREG_BAD_UTF8_OFFSET_ERROR] => 5
)
[#2] Harrison [2015-06-23 12:04:55]
DIRECTORY_SEPARATOR is a lifesaver! A hacker must never know what php files you have included into your php webpages or your php scripts that javascript calls upon. If you were to use '/' to include a file, the hacker can instantly know that the file you included is there, however, if you use DIRECTORY_SEPARATOR then the hacker would not know the file has been included.
[#3] Anonymous [2014-03-08 15:04:32]
In PHP 5.6 you can make a variadic function.
<?php
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");
?>
[#4] Anonymous [2013-09-29 16:09:27]
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');
?>
[#5] Paulo Marques [2013-09-16 14:31:46]
DIRECTORY_SEPARATOR is not necessarily needed, PHP always converts / to the appropriate character in its file functions.
It is good practice, though.