什么是更好的require(dirname(__ FILE __)。'/'。'myParent.php')而不仅仅是require('myParent.php')? [英] What's better of require(dirname(__FILE__).'/'.'myParent.php') than just require('myParent.php')?

查看:108
本文介绍了什么是更好的require(dirname(__ FILE __)。'/'。'myParent.php')而不仅仅是require('myParent.php')?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很多着名的PHP脚本,包括WordPress使用 dirname(__ FILE __)。'/ myParent.php'而不只是'myParent.php'将文件包含在当前运行脚本的同一目录中时。

Lots of famous PHP scripts including WordPress use dirname(__FILE__).'/myParent.php' instead of just 'myParent.php' when including files in the same directory of the currently running script.

它们不是同一个东西吗?为什么你更喜欢打字?

Aren't they the same thing? Why do you prefer typing more?

谢谢。

推荐答案

PHP需要知道文件的绝对路径。 dirname(__ FILE __)。'/ myParent.php'已经是绝对路径,但'myParent.php'需要一个使用 include_path <中的给定路径进行查找/ a>获取绝对路径并找到该文件。更好的选择是'./ myParent.php'

PHP needs to know the absolute path to the file. dirname(__FILE__).'/myParent.php' already is the absolute path but 'myParent.php' requires a lookup using the given paths in include_path to get an absolute path and find the file. A better choice would be './myParent.php':


但是,显式使用 include'。/ file'比让PHP总是检查每个包含的当前目录更有效。

However, it is more efficient to explicitly use include './file' than having PHP always check the current directory for every include.

这篇关于什么是更好的require(dirname(__ FILE __)。'/'。'myParent.php')而不仅仅是require('myParent.php')?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆