Search code examples
phpinclude-path

Diference between /../*.php and *.php


So I had this trouble with a PHP code that I solved, but I don't really know how it works.

I tried to add this to every single file

<?php include '/nav.php'; ?>

So I can only have one file to edit the navbar instead of the whole php files.

The problem came up when I added that code to the files of another folder (ie. /general/vision.php) and I got an error (file not found). I though it was because I only hav the 'nav.php' file on the root of the site.

So, just for fun, I changed the sentence of the /general/vision.php file to

<?php include '../nav.php'; ?>

And it worked. I didn't changed the nav.php from the root, and it worked.

I'm glad it did, but I don't know why. How does that /../ works? Is that listed on the PHP documentation?


Solution

  • .. is OS nomenclature for "up a directory." It's nothing PHP specific.

    Take a look at the concept of Relative paths.. might shed some light. Relative paths vs Absolute paths.