Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Enhancing a Simple PHP Application Cleaning URLs with Subfolders Introducing Constants

Brian Enos
Brian Enos
10,468 Points

Introducing Constants: Difference between "BASE_URL" and "ROOT_PATH

In this video, I'm a bit confused about the difference between the "BASE_URL", and ROOT_PATH" constants in this example.

<?php
define ("BASE_URL", "/shirts4mike_local/");
define ("ROOT_PATH", $_SERVER["DOCUMENT_ROOT"] . "/shirts4mike_local/");

Do these two constants not point to the same location? If not, what is the difference? Thanks for any help.

David Rynn
David Rynn
10,554 Points

To clarify the OP's question: if understand correctly, BASE_URL is for browser side (client side) paths. That is the operation will occur in the browser. ROOT_PATH is for server side operations - that is the operation (the include) will happen on the server side.

So for includes/requires in php, which is running on the server side, it needs a "ROOT_PATH". For links/ref, etc, where you're trying to include a css or javascript file, that occurs on in the browser, you would use "BASE_URL".

3 Answers

Ben Falk
Ben Falk
3,167 Points

The easiest way to test this would be to do a quick echo of both constants, and view the difference.

My understanding is that the two items have slightly different purposes.

-- The BASE_URL is useful when you need to use the project's URL for some reason, perhaps to include in a URL to a CSS or Javascript file.

-- The ROOT_PATH uses $_SERVER['DOCUMENT_ROOT'], which you can learn more about here: http://php.net/manual/en/reserved.variables.server.php. DOCUMENT_ROOT will give you absolute path to document root of the website in server's file system, for example, "/var/www/domain/", which would be useful for using PHP's include() function (or anything else that needs a file path), which has to use a path to the included file, and can't use a URL.

Hope that helps?

Pavle Lucic
Pavle Lucic
10,801 Points

Have problem with including config.php file.

In his video, Randy including config.php file into receipt.php (required_once is used), and that is fine. BUT, he doesnt include in header.php, and he calls defined CONSTANTS in it? Could someone explain Am I wrong ?

Cause you have missed that moment. Randy did include config.php in header.php

Kristi Smythe
Kristi Smythe
10,665 Points

Some local server configurations (mine) toss a fatal error with a config.php file name conflict, so I changed the config.php for this project to siteConfig.php