0

Say I am in foo/bar/index.php and want foo/index.php.

I require it by saying require_once '../index.php'; I saw somewhere that it is not good practice to do it like this, better to do prepend a slash making it require_once '/../index.php';

Can you explain me the difference and why one is better then the other?

Zevi Sternlicht
  • 5,399
  • 19
  • 31
  • 1
    did you mean **foo/index.php** ? –  Aug 29 '12 at 20:41
  • A leading slash makes the path absolute. For various reasons, the parent directory of root is root itself. So `/../index.php` is equivalent to `/index.php`. – tripleee Aug 29 '12 at 20:49
  • @tripleee how can this be? `/index.php` is in a different location to `/../index.php` – Zevi Sternlicht Aug 29 '12 at 20:54
  • `/index.php` is in the root directory. `/../index.php` is in the parent directory of the root directory. Like I mentioned, the parent of the root directory is the root directory. QED. – tripleee Aug 29 '12 at 20:57

1 Answers1

0

You're better off using /foo/index.php ../ will only take you one leve back. So, if your directory structure changes, you'll have to change all your links.

Build you links from the root forward, not the other way around if you can help it.

Paul Dessert
  • 6,363
  • 8
  • 47
  • 74