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 trialBrian Enos
10,468 PointsIntroducing 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.
3 Answers
Ben Falk
3,167 PointsThe 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
10,801 PointsHave 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 ?
Ngai Long Chan
2,862 PointsCause you have missed that moment. Randy did include config.php in header.php
Kristi Smythe
10,665 PointsSome 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
David Rynn
10,554 PointsDavid Rynn
10,554 PointsTo 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".