Search code examples
phpinclude-path

How PHP require file logic works?


File tree;

  • boot.php
  • panel/index.php
  • panel/inc/start.php
  • panel/inc/functions.php

Process:

  • index.php; requires 'inc/start.php'
  • start.php; requires '../boot.php' and 'functions.php'
  • (require_once 'x.php' used)

Everything works when index.php called. Why i don't have to use ../../boot.php instead of ../boot.php? If relative folder is /panel, then require 'functions.php' should fail. If it is /panel/inc, require '../boot.php' should fail. But eveything works. How?

Note: I know i should use absolute folder to include files. I am just trying to understand how this example works.


Solution

  • As for the PHP manual:

    http://php.net/manual/en/function.require-once.php

    http://www.php.net/manual/en/function.include.php

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

    When PHP includes / requires a file, it looks for a path relative to the current file position.