Question about Document Root

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SyntheticShield
Forum Newbie
Posts: 18
Joined: Tue Aug 19, 2008 2:46 pm

Question about Document Root

Post by SyntheticShield »

I am in the process of redesigning my website and I decided I would take my time and try and learn a few things. One of the things I wanted to learn was at least some basic php. Long story short, Im trying to use includes for content, menus and such. And in that process, it occurred to me that I had seen an example in a book where they used includes to build the page template as well so I thought why not.

Well, in the process of doing this the more pages I created the harder it became to keep track of how many '../' was I going to have to use to get the links to refer to the points I wanted in the site structure. A google search lead me to

Code: Select all

$_SERVER['DOCUMENT_ROOT'];
Which in turn led me to the use of:

Code: Select all

<?php include ($_SERVER['DOCUMENT_ROOT'].'/file/location.php');
That in turn got me to thinking, but unable to find a solution thus far is how can I declare document root globally for the entire page so that I dont have to write for each include

Code: Select all

($_SERVER['DOCUMENT_ROOT']
? Im not sure it can be done, just asking and keep in mind Im new to all this but trying to learn. I guess Im just trying to make the code more efficient and Im not sure there would be any reason to declare such a thing just one time for the whole page.

Anyway, I'll stop there and see what I need to do or if there are any options. Thanks in advance for your help.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Question about Document Root

Post by califdon »

Well, you could certainly assign it to a variable and then use that variable (all you're after is a string, after all), but you probably wouldn't save much code by doing so. I think it is pretty common practice to just use the $_SERVER['DOCUMENT_ROOT'] expression where needed.
SyntheticShield
Forum Newbie
Posts: 18
Joined: Tue Aug 19, 2008 2:46 pm

Re: Question about Document Root

Post by SyntheticShield »

Understood. I figured it couldnt help but to check. Im sure I'll be around with other questions as I dive into this a bit more.
Post Reply