php – Difference between './', '../' and '/'


In some codes I work with, sometimes there isn't a global variable that points to the project's root, such as:


So they often use:


What's the difference?

What happens to files from other directories that have been included (include) and need access to resources from the local directory?


|a.php -> inclui ('pasta/b.php') 
|pasta/b.php -> acessa ('img.png')


Each of these changes the way a directory is referenced.

Let's assume a teste.txt file:

  • /teste.txt : means that the file teste.txt is in the system root folder;

  • ./teste.txt : it means that the file teste.txt is in the same folder where the script is running;

  • ../teste.txt : it means that the file teste.txt is in the folder immediately above the folder where the PHP script is running.

In any programming language, it is always important to reference files in relation to the application's root directory, and not in relation to the system , to avoid confusion. With this you guarantee portability to the application, which can run on any system or directory structure, regardless of its file system.

Scroll to Top