Jump to content

cbox


Beginning PHP - Lesson 20 - Including files with PHP's Inclu


fattwam

Recommended Posts

Posted

Now that we know how to wield the POWER of PHP's file API lets kill some orc's!

uh.. wait...that's not, *quite* right...

 

*ahem

 

Actually, what I was going to say is:

Now that we know how to read and write to files let me show you how to include other PHP files in your scripts.

 

If you have ever coded in another language I am sure your saying "it's about time!". After all, including other files will become the basis for many of your scripts. If you think about it, it makes more sense to have a several different files that you can include if you want - as appose to having ONE huge file.

 

There are two main functions for including files into your script:

include();

require();

Basically, the only difference is that if include can't get the file you will get an error, but the script will still run. However, if require can't get the file your script will terminate with an error. Therefore, it is better to use include if you still want your script to keep going even if the file can't be opened.

 

 

So to get you used to including files I want you to open your text editor and type the following into it:


$number = 1;

if ($number == 0) {
   include("somedumbfile.php");
}

if ($number == 1) {
   include("saying.php");
}

echo "The included file says:
";
echo $saying;
?>

 

Save the file as "includetest.php" and then up load it to your web server. If you run it you will get an error saying that there is no variable called "$saying", so... open back up your editor and make a new file called "saying.php". Enter this into it:

 


$saying = "There are ten kinds of people in the world - those that understand binary, and those that don't.";

//Note: if you don't get the joke just enter something else...sheesh!
?>

 

Then upload this file to your web server and run "includetest.php" again. This time you will see "includetest.php" print out what you entered in "saying.php"! That is because the variable $saying was passed from the included file to "includetest.php":

 

The included file says:
There are ten kinds of people in the world - those that understand binary, and those that don't.

 

Now I am sure that you are starting to get ideas from this. I know that I was amazed when I first learned you could include files in PHP.

 

Honestly, I have a confession to make - I learned PHP just for the include function! :wink:

 

Up until then, I had only been using HTML for my sites. It was a MAJOR pain to open each and every file and change the navigation bar every time I added something. However, with PHP you can just make ONE navigation file that has the nav bar and then include it into every page that needs the nav bar. That way you only have to change "nav.php" and every page on your site will have the new nav bar! Trust me, this is an AWESOME feature if you have a site with 100+ pages - as there is no way that you could open all of them up and change the nav bar!

 

This site is a perfect example of using include to build a web page.

There is a header, content, and footer file on my server. In order to show this page PHP runs a script that looks like this:

 

include("header.php");
include("content.php");
include("footer.php");
?>

Now this is very different than what this page actually does, but the idea is the same. :wink:

 

Every time I open "header.php" and add a new thing to the banner area above all of the pages on my site then have the new code because they all include "header.php" before they show you the page.

 

 

So, now that you know about include lets build a simple page that is made up of different files that all come together to make what you see! Open up SciTE or Notepad and enter this into it:

 

echo "</pre><table border="1">Here is the Header:
";
include("header.php");
echo "This is the Content area:
";
include("content.php");
echo "Here you could put the copyright:
";
include("footer.php");
echo "</table>";<br>?&g

Then save it as "webpage.php".

Now you need to create three more files; "header.php", "content.php", and "footer.php". Create each one and put anything you want into them. Then take all four files and upload them to your web server. If you need ideas look at this:

 

 

// Header.php
echo "this is the header";
?>
// Content.php
echo "This is the stuff that the whole page is actually about!";
?>
// Footer.php
echo "&copy 2006 learnphpfree.com";
?>

 

 

After you have uploaded website.php, header.php, content.php, and footer.php; run website.php. You will see something like this:

 

Here is the Header:
this is the header
This is the Content area:
This is the stuff that the whole page is actually about!
Here you could put the copyright:
? 2006 learnphpfree.com

(with a border around each part!)

 

Well, this is nothing special so let me show you something more useful. You could change the "website.php" code to this:

 


include ("header.php");

///////////////////////////////////////////////////////////////
$pagetoget = $_POST['pagename'];
//Get the page that the user wants to see and put it in a var.

//Now we find the page that the user wanted to see and "include" it.
if ($pagetoget == "help") {
   include ("help.php");
}
elseif ($pagetoget == "about") {
   include ("about.php");
}
elseif ($pagetoget == "help") {
   include ("about.php");
}
else {
   // If no pages match just show the home page.
   include ("home.php");
}
///////////////////////////////////////////////////////////////

include ("footer.php");


?>

 

In this example, website.php is passed a page name and the script includes the corresponding page. In this way you can actually just use ONE page to build and print all of the pages on your site! That way you don't have to worry about re-coding the WHOLE page for every page you have. Just make part of a page with the content on it and BOOM! let you main page grab the "header" and "footer" pages and put it all together.

 

Like always, if you want to learn more about include() or require() stop by php.net and then do a search on Google. Later!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...