Document Root PHP

<a href="<?php echo $_SERVER['DOCUMENT_ROOT'].'/hello.html'; ?>">go with php</a>
    <br />
<a href="/hello.html">go to with html</a>

Try this yourself and find that they are not exactly the same.

$_SERVER['DOCUMENT_ROOT'] renders an actual file path (on my computer running as it's own server, C:/wamp/www/

HTML's / renders the root of the server url, in my case, localhost/

But C:/wamp/www/hello.html and localhost/hello.html are in fact the same file


Just / refers to the root of your website from the public html folder. DOCUMENT_ROOT refers to the local path to the folder on the server that contains your website.

For example, I have EasyPHP setup on a machine...

$_SERVER["DOCUMENT_ROOT"] gives me file:///C:/Program%20Files%20(x86)/EasyPHP-5.3.9/www but any file I link to with just / will be relative to my www folder.

If you want to give the absolute path to a file on your server (from the server's root) you can use DOCUMENT_ROOT. if you want to give the absolute path to a file from your website's root, use just /.


Yes, $_SERVER['DOCUMENT_ROOT'] contains the server side path which correlates to the client side URL path /. But, No they are not interchangeable.

They are not interchangeable because, for example, the server side path should never be sent to the client (HTML) side. The value of $_SERVER['DOCUMENT_ROOT'] is not /; it is the server's local file path to what the server shows the client at /. So, the value of ${$_SERVER['DOCUMENT_ROOT']}/images/thumbnail.png" may be the string /var/www/html/images/thumbnail.png on a server where it's local file at that path can be reached from the client side at the url http://example.com/images/thumbnail.png

note: $_SERVER['DOCUMENT_ROOT'] does not include a trailing /