Search code examples
phpconstantsdocument-root

Defining the document root directory as the ROOT_PATH


I use some constants to define paths to specific directories on a website that I'm building, like this:

define("ROOT_PATH", "http://website.com/");
define("IMAGES_DIR", ROOT_PATH . "images/");

and I usually use them like this:

echo "<img src='" . IMAGES_DIR . "some_image.jpg' />";

now I want to know if there is any difference between

define("ROOT_PATH", "http://website.com/");

and

define("ROOT_PATH", "/home/username/public_html/");

and which one of them should I use? And why?


Solution

  • You can't really use the following:

    define("ROOT_PATH", "/home/username/public_html/");
    

    since it will try to load

    http://website.com/home/username/public_html/image.png
    

    which you don't really want.

    Using

    define("ROOT_PATH", "http://website.com/");
    

    will try to fetch

    http://website.com/image.png
    

    which is what you want.