What does the dot-slash do to PHP include calls?

2019-01-11 05:46发布

A. What does this do?

require ("./file.php");

B. in comparison to this?

require ("file.php");

(Its not up-one-directory.. which would be)

require ("../file.php");

7条回答
趁早两清
2楼-- · 2019-01-11 06:14

There's a difference I noticed today.

Let's say you include a file in index.php.

<?php
include "./inc/head.php";
// right now, with the current code, this would work as well:
// include "inc/head.php";

tldr aclaration: it's slighly faster to use "./" because it wouldn't search in other folders first..

But now lets say I want to include the file inc/functions.php.

Because I will include this functions in every place I have the "head", I would directly include that file within inc/head.php.

<?php
// Wouldn't it be logical here to do this?
include "./functions.php";

The problem is, since the file where the script began is actually index.php and NOT inc/head.php, this script will fail because it would try to look for a file that doesn't exist: a file named functions.php in the same directory as index.php.

[+] inc/
  \_ [.] functions.php <- We want to include this...
   _ [.] head.php      <- ...from this file
[.] index.php
 x  functions.php <-- Not this! (doesn't exist)

So, instead of using the "./" sintax, we have two options:

  • Include the file using this code: include "./inc/functions.php";
  • Include the file using this code: include "functions.php"

So in this particular case, having or not a "./" actually is important, since it considerably changes how the script behaves.

Hope this was clarifying.

TLDR; Using "./" will always be relative to the file where the execution initialices, while using nothing ("") will be relative to the include_path, and after that, relative to the directory of the current file

查看更多
乱世女痞
3楼-- · 2019-01-11 06:17

./ is the current directory. It is largely the same as just file.php, but in many cases (this one included) it doesn't check any standard places PHP might look for a file, instead checking only the current directory.

From the PHP documentation (notice the last sentence):

Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script. E.g. if your include_path is libraries, current working directory is /www/, you included include/a.php and there is include "b.php" in that file, b.php is first looked in /www/libraries/ and then in /www/include/. If filename begins with ./ or ../, it is looked only in the current working directory.

查看更多
做个烂人
4楼-- · 2019-01-11 06:27

The Short Answer

You're right, it's not up one directory. A . refers to the directory you're in, and .. refers to the parent directory.

Meaning, ./file.php and file.php are functionally equivalent in PHP. Here's the relevent page of documentation: http://us.php.net/manual/en/wrappers.file.php

The Longer Answer

However, just because they work the same in this context doesn't mean they're always the same.

When you're operating in a *nix shell environment, and you type the name of an executable file, the shell will look in the PATH directories, but it won't look in the CWD, or the directory you're currently in.

So, if you're in a directory that has a file called: myprogram.php (this would be a PHP CLI file) and you just type:

myprogram.php

it doesn't matter if your program is executable or not. The shell will look in /bin/, /usr/bin/ etc for your file, but it won't look in ./, or the directory you're in.

To execute that program without adding your directory to the PATH, you need to type

./myprogram

So really, ./ is more explicit. It means, "the file you're looking for HAS to be right here" and no ./ means, "the file should be somewhere the program looking for files".

查看更多
放荡不羁爱自由
5楼-- · 2019-01-11 06:30

The dot-slash forces the file to be found in the current directory only, rather than additionally searching the paths mentioned in the include_path setting.

查看更多
看我几分像从前
6楼-- · 2019-01-11 06:30

Simply you are telling php to include the file in the current directory only or fail if the file is not present.

If you use the format "indexcommon3.php" and the file is not present php will search it into the include_path system variable.

For reference you can use http://www.php.net/manual/en/function.include.php

查看更多
地球回转人心会变
7楼-- · 2019-01-11 06:32

The first version forces the internal mechanism to include files relatively to the... directly executed file. So for example you have

index.php

// directly executed script (php -f index.php or from a browser)
include 'second.php';

second.php

// This is included relatively to index.php
// Actually, it is first searched relatively to include_path, then relatively
// to index.php
include './third.php';

third.php

// This is included relatively to second.php ONLY. It does not search
// include_path
return "foo";
查看更多
登录 后发表回答