in PHP what is the difference if i use ./ (dot sla

2019-08-13 16:10发布

问题:

would it affect the performance of the application if i use ./ when calling a class file in php or on any programming language.

example is: include "./includes/functions.php";

against: include "includes/functions.php";

回答1:

Write what you mean.

./foo means foo in the current directory.
foo means foo in the current or any directory in the include path.

It's an important difference in meaning, performance is irrelevant and negligible. If foo exists in the current directory, there'll be no difference.



回答2:

php docs on include:

If a path is defined — whether absolute (starting with a drive letter or \ on Windows, or / on Unix/Linux systems) or relative to the current directory (starting with . or ..) — the include_path will be ignored altogether. For example, if a filename begins with ../, the parser will look in the parent directory to find the requested file.

in the description of core php.ini directives it is mentioned, that

Using a . in the include path allows for relative includes as it means the current directory. However, it is more efficient to explicitly use include './file' than having PHP always check the current directory for every include.

but then again, you don't call include that often. performance overhead is negligible and not critical for your application. as Knuth has put it: »premature optimization is the root of all evil«



回答3:

There will be an insignificant speed difference due to the length of the string that you are using. In terms of style, using "./" implies that you intentionally mean a relative filename and have not just missed the leading slash out accidentally.



回答4:

I don't think there is much of a difference. Both produce de same opcode, and the path resolution is done within a well optimised C function in the PHP core or within a (very) well optimised C system call. Even if there is a difference it won't affect your overall performances since you rarely require hundreds of thousands of files…

EDIT: i've just run some test (including 10,000 files), and there is absolutely no difference, except that:

  • ./libdir/file.php will look for the file in the current folder
  • libdir/file.php will look for the file in the current folder and in the $PATH


回答5:

Both mean the same. If there is a differenece in PHP file IO system parsing it then it should be totally minimal and only be visible in large benchmarks. You can try that but I doubt that the difference would be significant.