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 Enhancing a Simple PHP Application Cleaning URLs with Subfolders Using Absolute Server Paths

Ben Attenborough
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Ben Attenborough
Front End Web Development Techdegree Graduate 32,769 Points

Why not just use /inc/footer.php instead of ($_SERVER["DOCUMENT_ROOT"] . "/inc/footer.php"

I'm unclear why we need to use ($_SERVER["DOCUMENT_ROOT"] . "/inc/footer.php" instead of a root relative link like /inc/footer.php

2 Answers

Kris Phelps
Kris Phelps
7,609 Points

Hey Ben,

It's because it falls within the PHP function of "include". PHP doesn't use the root relative like HTML does.

Try it in your project and you'll see. Put this into your /receipt/index.php:

include("/inc/footer.php");

It won't work. This is because PHP is a server side language. Meaning, it can see deeper into the server than your website's HTML can.

Your website might be hosted at http://localhost/ but your actual website, from the server perspective is at: c:\XAMPP\htdocs\

So, in PHP, when you do this:

include("/inc/footer.php");

PHP will actually be looking for c:\inc\footer.php. You can verify this by creating an "inc" directory in c: then putting a copy of footer.php into it. The error on receipt.php will go away and you'll see your footer.

I hope this helps explain things :-).

Luis Vazquez
Luis Vazquez
6,370 Points

Hi Ben - It looks like he's just using it as an example. In later videos, he actually uses variables from a config file. I originally thought the same thing though :-)