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

7

40

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");
Duda answered 23/2, 2009 at 21:11 Comment(0)
S
48

./ 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.

Subcartilaginous answered 23/2, 2009 at 21:15 Comment(1)
so, the require ("./file.php"); is faster than require ("file.php"); ?Lyophilize
M
9

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";
Melchor answered 23/2, 2009 at 21:15 Comment(2)
I had a few mistakes that I've now corrected, the order of search was wrong. The proof is the answer above mine.Imogene
In third.php comment is actually wrong. third.php is actually included relatively to index.php only. Actually, even that is wrong. It is actually included relatively to the current working directory only. For example, if index.php is located in /var/www/index.php. And you cd to other directory, (eg. cd /var) and then you try to execute index.php by php www/index.php, THEN when you do include './third.php', php will try to find it in /var directory and fail if it's not thereArchiepiscopal
G
7

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 relavent 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 ./: 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 lack of ./ means, "the file should be somewhere the program is looking for files".

Gains answered 23/2, 2009 at 21:21 Comment(0)
S
5

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.

Sacha answered 24/2, 2009 at 19:46 Comment(0)
S
5

Edit: I have completely rewritten the answer for the sake of clarity


When including a file, you can either use ./myfile.php or myfile.php.

They are not the same and you should always, preferably, use the first syntax, unless you know what you're doing.

The difference is best illustrated with an example: lets say you have the following files and folder structure:

index.php
inc/inner.php

From index.php, you can include your inner template without the ' ./' and it will work as expected:

# index.php
<?php
include "inc/inner.php";

Now let's say we add a new file, so the folder structure is now like this:

index.php
inc/inner.php
inc/inner-inner.php

To include inner-inner.php in inner.php, we would do this... right?

# src/inner.php
<?php
include "inner-inner.php";

Wrong. inner.php will just look for inner-inner.php in the root folder.

index.php
* inner-inner.php <- Doesn't exist, PHP ERROR.
inc/inner.php
inc/inner-inner.php

Instead, we should use the ./ syntax to include inner-inner. This will instruct that the file we are including must be included relative to the current file, not to the current "entry point" of the PHP script (index.php in the example).

# src/inner.php
<?php
include "./inner-inner.php";

In addition, and as mentioned in other comments, if you have configured a different "load path" for your PHP application, that load path will be looked up first without the ./ syntax. Instead, the ./ syntax is more efficient because it doesn't check the "include path" of php 1.

If you wanna make your includes even more explicit, use the __DIR__ constant. This tells php: "Use the directory of this file" 2.

# src/inner.php
<?php
include __DIR__ . "/inner-inner.php";

TLDR; Always use ./ or __DIR__ 2 because it's relative to the current (working) directory and doesn't depend on the PHP "include path" 1.


References:

  1. [Include - https://www.php.net/manual/en/function.include.php]
  2. Magic Constants - https://www.php.net/manual/en/language.constants.magic.php
Streamlet answered 5/6, 2018 at 17:35 Comment(1)
You saved me typing a new answer, thank you. This is by far the best answer, because while most developers don't care for that extra millisecond gained when not searching in include_path, the biggest problems and misunderstanding with PHP in these here regard arise when nesting several includes. Upvoted.Gorse
K
4

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

Khichabia answered 23/2, 2009 at 21:16 Comment(0)
Y
0

It's explicitly naming the current directory.

Yokefellow answered 23/2, 2009 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.