Take the 2-minute tour ×

Just to confirm, is using:


the same as using: /

in HTML.

Eg. If current document is:


I could use (in HTML) to start at the roort:


and to do the same in PHP I would have to use:

$_SERVER["DOCUMENT_ROOT"] . "/somedoc.html";

Is that correct? Is there an easier way to do it?

share|improve this question

3 Answers 3

up vote 6 down vote accepted
 <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

share|improve this answer
if you want to use it say for include(), its a different issue than for a hyper-link above. –  Dagon Aug 13 '12 at 4:02
No reason to post my answer since this one is what I was going to say. –  Joe C. Aug 13 '12 at 4:02
What's different with includes? –  Andrew Aug 13 '12 at 4:02
You'll need a / between DOCUMENT_ROOT and hello.html –  sachleen Aug 13 '12 at 4:02
file path and url are not the same thing –  Dagon Aug 13 '12 at 4:03

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 /.

share|improve this answer

The Easiest way to do it is to have good site structure and write it as a constant.

share|improve this answer
i know its old but i stumbled upon this, why would you make more constants if you already have a php defined server constant doing this, waste of code, cpu and memory –  DarkMukke Nov 4 '13 at 10:35
The idea is, no matter how far deep (folder-wise) you are in includes, this makes it so you'll never have to write code relative to the script, other than the code used to define the root. I use this to combat the Jerry-rigging method used by many programmers today. –  Adam F Nov 5 '13 at 3:37
yes but that way you script becomes depended on the system or on the location, which it should never be, apps should be portable –  DarkMukke Nov 6 '13 at 14:56

Your Answer


