Hello,
I’m not having any code issues per se, but do have a question more about cleanliness of code. Now at the tail end of my first big experience with php/designing a site, I’m reflecting on some efficiency issues. One issue that’s come up is whether it would have been more efficient to use absolute instead of relative paths.
For example, I originally wrote
if ($member_logged_in) {
if ($parts[1] == "index.php") {
include('includes/all_pages_nav_left.php');
} elseif ($parts[1] == "Chapters") {
include('../../includes/all_pages_nav_left.php');
} elseif ($parts[1] == "Students" || $parts[1] = "Instructors") {
include('../includes/all_pages_nav_left.php');}
}
if (!($member_logged_in)) {
if ($parts[1] == "index.php") {
include('includes/index_nav_left.php');
} elseif ($parts[2]=="Homepage" || $parts[2] =="Linear_Functions") {
include('../../includes/index_nav_left.php');
} elseif ($parts[1] == "Students") {
include('../includes/index_nav_left.php');}
}
which works just fine. But, now I’ve “discovered” $_SERVER[DOCUMENT_ROOT] and the code
if ($member_logged_in) {
include ($_SERVER['DOCUMENT_ROOT'] . '/includes/all_pages_nav_left.php');
} else {
include ($_SERVER['DOCUMENT_ROOT'] .
'/includes/index_nav_left.php');
}
seems to work just as well. Initially, Ithought that I’d have to hard-code the absolute path: with different servers (local vs. remote for example), it would have been a nightmare. But with the $_SERVER variable, I think that there’s no reason that I ever should have used relative paths for anything such as above.
So, my question is whether there would ever be compelling reason to use relative over absolute paths with PHP…
Thanks!
-Eric