在 PHP 5.6 中,您可以建立可變參數函式。
<?php
/**
* 建立具有適當目錄分隔符號的檔案路徑。
* @param string $segments,... 無限數量的路徑區段
* @return string 路徑
*/
function file_build_path(...$segments) {
return join(DIRECTORY_SEPARATOR, $segments);
}
file_build_path("home", "alice", "Documents", "example.txt");
?>
在較早版本的 PHP 中,您可以使用 func_get_args。
<?php
function file_build_path() {
return join(DIRECTORY_SEPARATOR, func_get_args());
}
file_build_path("home", "alice", "Documents", "example.txt");
?>