Should a directory path variable end with a trailing slash?

前端 未结 12 2042
暖寄归人
暖寄归人 2020-12-24 04:33

When defining a path to a directory as a variable or constant, should it end with a trailing slash? What is the convention?

pwd in Unix shows your curre

12条回答
  •  再見小時候
    2020-12-24 04:43

    I know this is an old thread but I thought I'd share what I do. If possible, I'd normally allow for both and do something like this (if it was PHP):

    $fullPath = rtrim($directory, '/') . '/filename.txt');
    

    That way, if the directory is defined in a config file, it doesn't matter whether the next person to change it includes the trailing slash or not.

提交回复
热议问题