Should a directory path variable end with a trailing slash?

前端 未结 12 2076
暖寄归人
暖寄归人 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:48

    In php, since dirname(__FILE __) function returns the directory name without a slash at the end. I tend to stick to that convention.

    Otherwise, using a slash at the end of a directory name will conflict with the way dirname(..) works and then you are stuck with handling the two cases since you don't know if the directory name came from a dirname(..) function or a content defined with a trailing slash.

    Bottom Line: Don't use a trailing slash since dirname(..) doesn't.

    // PHP Example
    dirname(__FILE__); // returns c:\my\directory without a trailing slash, so stick to it!
    

    For other languages, check the function that extracts a pathname, and see if it is using a trailing slash or not, then stick to the language's convention.

提交回复
热议问题