I have read that when including a php file that using absolute paths has a faster processing time than relative paths.
What would you suggest to use?
include("includes/myscript.php");
or
include("/home/ftpuser/public_html/includes/myscript.php");
or even
set_include_path("/home/ftpuser/public_html/includes");
include("myscript.php");
Or is it something that I really shouldn't worry about?
Relative pathsIf you don't supply the root, it means that your path is relative. The simplest example of relative path is just a file name, like index. html . So one should be careful with relative paths. If your current directory is /about/ then index.
An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).
If you do a lot of testing and move content frequently between domains, then relative links are the best solution. Absolute paths are more secure, protect content, and prevent duplicate pages. The main point is that the link format you prefer should be applied to all URLs on the site.
The __DIR__ can be used to obtain the current code working directory. It has been introduced in PHP beginning from version 5.3. It is similar to using dirname(__FILE__). Usually, it is used to include other files that is present in an included file. Consider the following directory structure −
I usually set a constant, either manually or like this:
define('ROOT', dirname(__FILE__));
Then do
require ROOT . '/include/file.php';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With