-1

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.

jcjr
  • 1,503
  • 24
  • 40
previous_developer
  • 10,579
  • 6
  • 41
  • 66

2 Answers2

2

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.

Tomasz Kowalczyk
  • 10,472
  • 6
  • 52
  • 68
  • I was going to link to the manual also, you beat me to it. =) – diggersworld Nov 27 '12 at 18:56
  • Actually the part says "finally check in the calling script's own directory and the current working directory before failing" answered my question. Because i was thinking it takes only one directory to check as relative. Thanks. – previous_developer Nov 27 '12 at 19:19
0

Your index.php file, while processing, will include inc/start.php. So your index.php will look like

<?php 
 //inc/start.php file content here
 //require '../boot.php';

So the boot.php will be included like it including from index.php itself

Dmitriy Sushko
  • 242
  • 1
  • 6