What's better of require(dirname(__FILE__).'/'.'myParent.php') than just require('myParent.php')?

datasn.io picture datasn.io · Feb 8, 2010 · Viewed 30.7k times · Source

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?

Thanks.

Answer

Gumbo picture Gumbo · Feb 8, 2010

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':

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