PHP absolute root path and root url

PHP

Im trying to define the absolute root path and the root url of my app.
I tried this, however I can't understand something yet.

I have created config.inc.php in the root folder with the code in the answer.
Also I have the file index.php in the root in which I include config.inc.php and the constanst are fine. But I have another file in ${ROOT}/example/file.php where I also include config.inc.php but the root url contains the example folder in the end.

The app structure is:

config.inc.php
index.php
example/file.php

How can I always have the absolute root path and the root url of my app?

Thanks!

Best Answer

Absolute root path:

$_SERVER['DOCUMENT_ROOT']

Path from the root folder:

dirname($_SERVER['PHP_SELF'])