gpt4 book ai didi

php - $absolutePath = realpath(__DIR__) 中的 "realpath"是多余的吗? __DIR__ 总是规范的绝对路径吗?

转载 作者:可可西里 更新时间:2023-11-01 12:52:35 27 4
gpt4 key购买 nike

做这样的事情是不是一种感觉

 $dir = realpath(dirname(__FILE__));
// or for php 5.3+:
$dir = realpath(__DIR__)

或者像 DIRFILE 这样的魔术常量总是返回绝对路径,所以 realpath 是多余的?

最佳答案

您不需要realpath()

__FILE__ 的文档:

The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path with symlinks resolved whereas in older versions it contained relative path under some circumstances.

所以 __FILE__ 返回一个绝对路径 => realpath() 不必要的

如果你使用 __DIR__:

The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.)

__DIR__ 也返回绝对路径,因为 dirname() 没有截断 __FILE__ 路径的绝对部分 => < em>realpath() 不必要

关于php - $absolutePath = realpath(__DIR__) 中的 "realpath"是多余的吗? __DIR__ 总是规范的绝对路径吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9415940/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com