Include Looks in Two Locations?

My root/index.php includes root/Pages/home.php, which has the following statement:

include(“header.php”);

The functionality of this include varies. If there’s a file located at root/header.php, that’s the file that gets included. Otherwise, it loads root/Pages/header.php, which is what I would expect to be the default behavior.

Can anyone explain what’s going on? I didn’t see any clear explanation in the documentation, but I can imagine it’s because the executed file (index.php) is in root rather than Pages.

Also, I suppose this means I take a tiny efficiency hit if I don’t use the full path?

Thanks,
e39m5

The include documentation says:

Files are included based on the file path given or, if none is given, the include_path specified.

The include_path documentation says:

[indent]include_path=“.:/php/includes”
Using a . in the include path allows for relative includes as it means the current directory.
[/indent]

Not really sure this answered my question. What causes it to look in two separate locations?

Thanks,
e39m5

It looks in more than two separate locations.

If the file cannot be found, it then checks all of the paths in the include_path.

It is one of those paths in the include_path that is responsible for your experience with the second location.

i am also not 100% sure about the ?
but if question is (what i feel is)
root
header.php
folder1
header.php
file.php

and in file1 header of folder1 is included rather than header of root

then
using this
require_once($_SERVER[‘DOCUMENT_ROOT’].‘/header.php’);
can come handy
some use site_url config varibale but this is more generic i guess…

NS:sorry if i misunderstood the ?

I think that the only thing needing to be resolved is the path that is used.

FileA includes header.php
The header.php is included from the same path as FileA. That much is obvious.

FileB.php includes path/FileA.php
Does FileA attempt to include header.php from the path of FileB or the path of FileA

My suspicion is it’s from the original execution path, but is that right?

PHP is a computer.
Therefore, it does things in order.
When PHP reaches an Include statement, it tries to immediately access the file located in the parenthses, if it has a relative address (‘./filename.php’).
If it fails to find the file there or if the address is indeterminately relative, it starts at the beginning of it’s Include-Path (Divided up by the colons). 99.999999% of the time, that’s in “.” (Current Executing Directory). Then /php/includes (Or wherever the person points it.) and so on. If it reaches the end of the include_path configuration setting without finding the file, it throws an error and moves on. (Include failure is E_WARN level, whereas Require() failure is Fatal)

With a relative path, PHP will:

  1. look for the file in the cwd - getcwd()
  2. look through the include path for the file
  3. look in the calling script’s directory for the file