简体   繁体   English

require(__ DIR__。'/file.php')和require('file.php')之间的区别

[英]difference between require(__DIR__ . '/file.php') and require('file.php')

As asked in the title, whats the difference between 如标题中所述,两者之间有什么区别

require(__DIR__ . '/file.php')

and

require('file.php')

?

(when both files are in the same folder) (当两个文件都在同一文件夹中时)

Thank you all for your help! 谢谢大家的帮助!

If you do 如果你这样做

require(__DIR__ . '/file.php')

then you are requiring the file with the full pathname. 那么您需要使用完整路径名的文件。 If the file doing this require is required by another file in another directory, this require will always work. 如果另一个目录中的另一个文件需要执行此要求的文件,则此要求将始终有效。 On the other hand, if you 另一方面,如果您

require('file.php')

then if the file where this require statement is is required by another file in another directory, this statement will fail. 然后,如果另一个目录中的另一个文件需要此require语句所在的文件,则此语句将失败。

That is why it is generally good practice to include the __DIR__ . 这就是为什么通常将__DIR__包含在内的__DIR__

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

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