Jump to content
Stef's Coding Community

How should I use PhP includes?

Recommended Posts

I am developing a website using PhP includes. Each folder and sub-folder pages are ‘index.php’ with the following structure:


<div id="container">

<div id="TopDiv">


include ("TopDIV/IncludeTop.php");


<div id="NavigationDiv">


include ("Menu/IncludeMenu.php");



<div id="CentralDiv">


include ("MainDIV/IncludeMain.php");



<div id="RightDiv">


include ("RightDIV/IncludeRight.php");





Naturally, for sub-pages the “CentralDiv” and “RightDiv” contains the php pages from appropriate folders. My questions:

1. The above works cool but I’m not sure whether this is the right way of doing things.

2. I feel that when a menu item is clicked then the content of ‘TopDIV” and “NavigationDiv” are re-loaded.

3. A related problem is that I cannot keep a menu item with an ‘active’ status. I think it does not show the ‘active’ status as the content of the “Navigation” DIV is loaded every time a menu item is clicked.



An ALTERNATIVE could be:


<div id="Navigation">

<a href="?go=home">Home</a><br /><br />

<a href="?go=page-1">Page 1</a><br />

<a href="?go=page-2">Page 2</a><br />

<a href="?go=page-3">Page 3</a><br />


<div id="content">


$default = "content/home.php";

if (isset($_GET['go'])) {

switch ($_GET['go']) {

case "page-1": include("content/page1.php"); break;

case "page-2": include("content/page2.php"); break;

case "page-3": include("content/page3.php"); break;

default: include($default);


} else { include($default); }





My questions for the alternative:

1. Is it a better way of doing than the first approach?

2. A related problem is the URL looks crazy (with “?go....” etc.)


Any advice will be appreciated.

Share this post

Link to post
Share on other sites

You should use includes the way they make the most sense to you.


In my case, if I am working on a static HTML/CSS site (nothing that requires a CMS or extensive PHP), I would use them to hold any HTML that will repeat across all pages - headers, footers, etc. This makes it easy to change that code if necessary - one change to the include file will change all files that use the include.


Personally, I wouldn't code the site the way you have above in sample #1. In my mind, the IncludeMain.php include is unnecessary (though the rest of the includes are fine.) I would place the code from that file directly within the index.php file. If the contents will be unique per page, I don't see the point of making it an include (the content will only appear in that one page, and you won't be including it elsewhere.)


Regarding the menu... there are two ways to approach this:


1) Use a function. You pass in a number that indicates which nav item should be active, and the function generates and returns the correct code. For example:


function create_nav($active)
   $data = '<li';
   if ($active == 1) { $data .= ' class="active"'; }
   $data .= '><a href="#">Nav item #1</a></li><li';

   if ($active == 2) { $data .= ' class="active"'; }
   $data .= '><a href="#">Nav item #2</a></li><li';

   if ($active == 3) { $data .= ' class="active"'; }
   $data .= '><a href="#">Nav item #3</a></li>';

   return $data;

// This would create a nav with the first item being active. You could then use CSS to style active items.

<ul class="nav">
   <?php echo create_nav(1); ?>

2) One other way would be to set a variable at the top of your index.php file (before your includes):


<?php $nav_active = 1; ?>

and then within your navigation include:


<ul class="nav">
   <li <?php if ($nav_active == 1) { echo ' class="active"'; } ?>><a href="#">First item</a></li>
   <li <?php if ($nav_active == 2) { echo ' class="active"'; } ?>><a href="#">Second item</a></li>
   <li <?php if ($nav_active == 3) { echo ' class="active"'; } ?>><a href="#">Third item</a></li>

Either way should work.

Share this post

Link to post
Share on other sites

Thanks, but I still have a few questions.

1. About index.php: You are absolutely right that there is no point using 'IncludeMain.php' as this page is not going to be included anywhere else. I will correct that. I am still curious about your comment: "Personally, I wouldn't code the site the way you have above in sample #1". As I am a beginner I would like to know what alternative I can do.


2. When all subpages are index.php (each with an include menu.php) then each time a page is loaded the menu.php is re-loaded (I think). Is it not a bad design?


3. Thanks for the codes for active menu item. Is it not possible to show the active menu just with the CSS link styles (hover/active/visited)?

Thanks in advance.

Share this post

Link to post
Share on other sites


When I said "Personally, I wouldn't code the site the way you have above in sample #1" I was simply referring to the fact that you used an unnecessary include.



I don't see any problem with it. The only way around that is to use frames or iframes, so only parts of the page refresh, which is generally frowned upon because of a wide range of issues (bookmarking, accessibility, etc.) Includes aren't intended to be used like iframes.



Not with the standard a:link/visited/hover/active. One way or another, you need a CSS class, either on the active navigation item, or somewhere else on the page to indicate which is the current active page.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...