Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Charles Coop
Charles Coop
3,045 Points

Difference between ROOT_PATH and BASE_URL?

In the video introducing constants, we created two constants, ROOT_PATH and BASE_URL. I'm not quite grasping the concept on the difference and which time would be appropriate to use one over the other. Can someone explain it differently? Thanks.

7 Answers

Kevin Korte
Kevin Korte
28,148 Points

I can see why you'd be confused easily. Basically, we are setting these two terms ourself. You could set them to what you want. But lets just explain it in the way Randy does here.

The first one:

define("BASE_URL","/");

We are setting BASE_URL to "/" which is the root of our site. I use this when I'm linking in CSS, JS, images, etc. So I might type something like this in my php file.

<img src="<?php echo BASE_URL; ?>images/path-to-my-photo.jpg">

When this gets printed to the screen. We set BASE_URL to root, so it's going to go to www.mydomain.com/images/path-to-my-photo.jpg

Lets say for any reason we moved on root into a sub folder. We could go back to our include file and change the BASE_URL to match. Like so

define("BASE_URL","/subfolder/");

This would than update all of our links for us, so nothing should break. The new image path would be: www.mydomain.com/subfolder/images/path-to-my-photo.jpg

Now, ROOT_PATH we define for our include files. I do not use ROOT_PATH nearly as much. We set ROOT_PATH as:

define("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . "/");

This will go to the ROOT of the server. This is very helpful incase we every change to a different host or server, this path is likely going to change, and everything depending on this link would break on our site. The DOCUMENT_ROOT will actually end up being something like:

www/var32/public_html/.....

Whatever the file system is for your server to your root. Like I said, I use this to include headers and footers when I'm on the index page of a folder or subfolder. So I might have something like:

<?php include(ROOT_PATH . "inc/footer.php"); ?>

This will always route to the ROOT of my server, and than from there will look for an "inc" folder, and than a footer.php, or maybe a header.php. That's about the only two times I use it.

So all of that to say: I use ROOT_PATH to include header, footer, or other include files, and BASE_URL to create anchors links, show images, link in CSS or Javascript, etc.

Hope that helps.

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Everything Kevin says is correct. I would only add one further point of clarification. If you the web page in the browser, you will see the values of BASE_URL there. That's why it applies to things like CSS, JavaScript, links, etc. You are telling the browser what the web address is for the root of the website.

<img src="<?php echo BASE_URL; ?>images/path-to-my-photo.jpg">

If you view the web page in the browser, you will not see the values of ROOT_PATH. That constant is used on the server to pull together the code needed to generate the HTML. You use it for include files like this ...

<?php include(ROOT_PATH . "inc/footer.php"); ?>

... but you don't see this when you look in the source code in the browser. The browser doesn't even realize that you have a shirts.php and a footer.php on the server; it just asks for the web page at shirts.php and gets one piece of HTML back.

Does that help?

Craig Salmond
PLUS
Craig Salmond
Courses Plus Student 7,438 Points

I'm having a VERY hard time understanding this as well. The above explanations helped a lot, but I get even more confused when I downloaded the project files and tried to use them to understand what was going on. All of the pages work except the shirts.php page, which isn't loading the styles at all. I can't see a reason for this weird behavior, but it's definitely there, and it's compounding my misunderstanding of subdirectories and including files with the BASE_URL and ROOT_PATH constants. I should also point out that in my own files I can get everything working except for the receipt page, which also isn't loading any files. I'm probably the common denominator in all of these errors.

Kevin Korte
Kevin Korte
28,148 Points

What does your link for your styles on your shirts.php look like?

Try setting up your own very simple folder structure. I'd do this. Start a fresh folder in your htdocs and name it whatever you want, like "testproject"

Inside of there, create two folder, one called "inc" and one called "css". In your "inc" folder, create your config.php file and set your BASE_URL and your ROOT_PATH.

In your "css" folder, create a new stylesheet, and do something simple like set the body color to something.

Now outside of your inc and css folders, create a new php document, and in your head link in your style sheet using the BASE_URL. When you navigate to that page in your browser, you should see the body color you set in your CSS.

This is a great way to troubleshoot since there is very little code, and very few moving parts. From here, no matter how complicated the code, this procedure stays the same.

Luke Lee
Luke Lee
7,577 Points

I am still confusing. if my domain name is www.mydomain.com <img src="<?php echo BASE_URL; ?>images/logo.jpg"> AND <img src="<?php echo ROOT_PATH; ?>images/logo.jpg">

Are they same in html? which is www.mydomain.com/images/logo.jpg ?

Kevin Korte
Kevin Korte
28,148 Points

This confused me. Do you mean if you have a folder named images, with an image named logo.png inside of it?

Luke Lee
Luke Lee
7,577 Points

sorry, the code was missing. <img src="<?php echo ROOT_PATH; ?>images/path-to-my-photo.jpg"/> <img src="<?php echo BASE_URL; ?>images/path-to-my-photo.jpg"/>

Above 2 image links will output the same result, right?

Luke Lee
Luke Lee
7,577 Points

Why my code was shortened? some codes are missing.

Kevin Korte
Kevin Korte
28,148 Points

Try putting your code between four (4) tick marks ```` (the button next to the number one key).

Like so (put a line break here)

This is my code

You have to close it with four more tick marks. The forum strips away tags when you put code into the reply box. It's a safety thing.

Luke Lee
Luke Lee
7,577 Points

Thank you, Kevin: my question is <img src="<?php echo BASE_URL; ?>images/path-to-my-photo.jpg"> AND <img src="<?php echo ROOT_PATH; ?>images/path-to-my-photo.jpg"> Seems they output same results?