简体   繁体   English

这两行PHP是不同的?

[英]Are these two lines of PHP different?

I'm looking through a code base. 我正在寻找代码库。 This is one of the lines. 这是其中一条线。

require_once( dirname(__FILE__) . '/a_script.php' );

I'm considering rewriting it as: 我正在考虑将其重写为:

require_once( './a_script.php' );

Are those two lines of code any different? 这两行代码有什么不同吗?

Yes, they are different. 是的,他们是不同的。 The second one is relative to the working directory, which is not always the same thing as the directory the currently executing file is in. 第二个是相对于工作目录,它与当前正在执行的文件所在的目录并不总是相同。

For the most part, the working directory is the directory of the original file . 在大多数情况下,工作目录是原始文件的目录。 If you are running the require_once in a file that is being included by another file, the path is relative the first file. 如果在另一个文件包含的文件中运行require_once,则该路径相对于第一个文件。

They have some different 他们有一些不同

// '/dir/index.php'
require_once( dirname(__FILE__) . '/script.php' ); // '/dir/script.php'
require_once( './script.php' ); // '/dir/script.php'

chdir('/')
require_once( dirname(__FILE__) . '/script.php' ); // '/dir/script.php'
require_once( './script.php' ); // '/script.php'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM