Saturday, December 8, 2012

PHP Includes - PHP Lesson 4

PHP Includes
By Stephon Unomon

 

       A PHP include is used when you want to include the contents of one file inside another...a very useful command!

 

<?php

include("file.inc");

?>

Real World Usage for Includes

 

       Let’s say you are developing a 5 page website that you might be adding pages to. Your navigation HTML looks like this:

 

<html>

<head>

<title>My Navigation</title>

</head>

<body>



<a href=http://www.mysite.com/articles.php>Articles</a>


<a href=http://www.mysite.com/contact.php>Contact Us</a>

</body>

</html>

 

...and this is going to be in every page. Now, when you add a page to your website, you are going to have to change the code on all 5 pages to reflect the new link. This could take several minutes to do...and what if the site expands to 20 pages or more? It’s going to be a nightmare!

 

PHP Includes to the rescue!

Turn the page and watch PHP save our developer from certain doom!

 

       Let’s cut the menu link code out of the example above and paste it into a new plain text document (You can use Notepad on Windows or TextEdit on the Mac for this).

 


<a href=http://www.mysite.com/products.php>Products</a>



<a href=http://www.mysite.com/contact.php>Contact Us</a>

 

       Save this text file as navigation.inc. Paste the following in place of where the code was in the original HTML files...

 

<?php

include "navigation.inc" ;

?>

 

       Then save the HTML page as a PHP page. Voila! Any time you need to change the menu links, all you have to do is edit one file - the navigation.inc file!

 


A Note on Includes...
You don’t have to use .inc as the extension for an include - you can include almost any type of file with a PHP include - HTML, PHP, even other URL’s! One thing to keep in mind, however...is to strip out all the formatting code from the page you are including from (like the <html> and <body> tags). In other words, only include exactly what you need!
 

A Note on Includes...
You don’t have to use .inc as the extension for an include - you can include almost any type of file with a PHP include - HTML, PHP, even other URL’s! One thing to keep in mind, however...is to strip out all the formatting code from the page you are including from (like the <html> and <body> tags). In other words, only include exactly what you need!



 
 

 

I Require You To Include   

 

In PHP, you can also use the “Require” command in place of Include. The major difference between the two is that using Require will stop the script from running (the page won’t load completely) if it cannot find the page that is referenced for inclusion. An Include will allow the page to load, but it will just ignore the included code if it cannot be found.

 

 

 

 

Now let’s mesh it all together in an example!

 

       Imagine this...you are building a website. Your Greatest Undertaking Of A Website. 200 Pages. 400 Articles. Writing Code By Hand. The Titan Of Websites. You want to put advertising on each page (We’ll use GoogleTM AdSense as an example). You also want to test different color themes on the ad code, or change the ads periodically. You are also going to be quite generous and give copies of this website away for others to use.

 

       The questions start filling your mind...how can I easily change the ad colors or format? How can I let other people easily put in their AdSense publisher code?

 

We’ll use Variables and Includes to solve the problem!

 

Open up 2 blank text documents. The first one is going to be a settings file, the second one is going to be your Ad code.

 

In the settings file, you are going to want to set variables for things you know you are going to want to change, the main thing being the AdSense publisher code and the ad link colors. So, we’ll set the variables as below...

 

<?php

$ad_pub_num = “pub-0123456789”;

$eb_linkcolor = “006699”;

?>

The variable, $ad_pub_num, now reflects the AdSense publisher tracking code, and the link colors are the HTML color code 006699, which is a dark blue. Save this page as settings.php.

 

Now, grab your AdSense code snippet from Google...

 

<script type="text/javascript"><!--

google_ad_client = "pub-0123456789";

google_ad_width = 120;

google_ad_height = 600;

google_ad_format = "120x600_as";

google_ad_type = "text_image";

google_ad_channel = "1";

google_color_border = "FFFFFF";

google_color_bg = "FFFFFF";

google_color_link = "006699";

google_color_text = "006699";

google_color_url = "006699";

//--></script>

  <script type="text/javascript"


</script>

 

And paste it into the other open document...making some changes...

 

<?php echo "

  <script type=\"text/javascript\"><!--

google_ad_client = \"$ad_pub_num\";

google_ad_width = 120;

google_ad_height = 600;

google_ad_format = \"120x600_as\";

google_ad_type = \"text_image\";

google_ad_channel = \"1\";

google_color_border = \"FFFFFF\";

google_color_bg = \"FFFFFF\";

google_color_link = \"$eb_linkcolor\";

google_color_text = \"$eb_linkcolor\";

google_color_url = \"$eb_linkcolor\";

//--></script>

  <script type=\"text/javascript\"


</script>

?>

 

...and save this file as ads.php.

 

 

 

Do you recognize the changes we made? We’ve told PHP to echo the code to the screen, and put in the variables that we set in the settings.php file. Since we are doing an ECHO, we’re displaying the quotation marks in the output with the \” code (That’s the way the AdSense snippet works). Now we are ready to call these files from the main web pages!

 

At the beginning of your content pages, you will want to add this code:

 

<?php

require “settings.php”;

?>

This tells the page that it requires the contents of Settings.php file to be included. Including this code on each page sets the variables we will use throughout the site.

 

Now, call the ads in the places you want them:

 

<?php

include “ads.php”;

?>

This will pull in the code from the ads page we made, including the variables we set, and show those ads wherever you care to place them. So, your ad link colors will appear in a dark blue and the example publisher number - pub-0123456789 - will show up in the ads. Name each one of your content pages as .php instead of .html, and you’ll be good to go! If you decide you want to change colors of the ads (or change the AdSense publisher ID code), you only have to make that change in ONE file instead of 200 - the settings.php file!


PHP Components

No comments:

Post a Comment