php relative and absolute paths

2020-02-05 03:02发布

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?

8条回答
家丑人穷心不美
2楼-- · 2020-02-05 03:32

I tend to setup my include directories / libraries by setting the include path on the initialisation of my app.

set_include_path("/home/ftpuser/public_html/includes");
include("myscript.php");

The zend framework does something similar to load the library classes.

查看更多
我想做一个坏孩纸
3楼-- · 2020-02-05 03:43

I wrote a simple speed test script using microtime(true). It tests the following five including methods with one million iterations:

// Absolute path.
include('/home/ftpuser/public_html/includes/myscript.php');

// Predefined path.
define('PATH', '/home/ftpuser/public_html/includes/');
include(PATH . 'myscript.php');

// Relative path.
include('myscript.php');

// Using set_include_path().
set_include_path('/home/ftpuser/public_html/includes/');
include('myscript.php');

// Superglobal path.
include(dirname(__FILE__) . '/myscript.php');


Which gave the following results (in seconds):

    Absolute path:            263.222
    Predefined path:          263.545
    Relative path:            301.214
    Using set_include_path(): 302.396
    Superglobal path:         269.631


My opinion based on these results is to use a predefined path, because it's the fastest only surpassed by an absolute path. However, an absolute path has the drawback that it must be altered in every file when a change is necessary.

Hope this helped. :)

P.S.
define and set_include_path() were used only once during execution of the script (they are located outside of the loop).

查看更多
登录 后发表回答