Saturday, July 2, 2011

PHP: Reusing your codes...

I'm sure back in the 90s, probably when you are creating a website, you might have create a webpage and replicate various parts of the webpage to another webpage in order to make your layout consistent. This might be pretty easy during the first 10 or 20 pages of the website. However as your website bigger and bigger, this might get more and more tedious and much more time will be needed for debugging and testing. That is why I'm going to show you how you can reduce all these crazy work if you are going to build a new PHP website. (Why PHP? Because it's free.)


In PHP, there is this special function known as 'include()' and by using this function, you can reuse a block of PHP codes across all your PHP webpages.

The following is a series of PHP files that I had created.

home.php
<?php
 //set the value of $section
 $section = "home";
 //include the header file to recycle your codings
 include("header.php");
 //Just display a simple message
 echo "Contents of ".$section." comes here.";
?>

contact.php
<?php
 //set the value of $section
 $section = "contact";
 //include the header file to recycle your codings
 include("header.php");
 //Just display a simple message
 echo "Contents of ".$section." comes here.";
?>

If you compare the contents of home.php and contact.php, you could easily identify the similarities and differences between both files and if you have taken a closer look, you could easily identify that I had included a php file named "header.php". This is the file that I will be reusing throughout my webpages.

And here's the source file for "header.php"
<?php
 //Create a table and base on the value of $section, toggle the state of the selected button.
 echo "<table cellpadding='0' cellspacing='0' border='0' width='100%' ><tr>";
 //If $section == home, make the text 'Home' unclickable else make it into a clickable hyperlink
 if ($section != "home"){
  echo "<td align='center'><a href='home.php'>Home</a></td>";
 }else{
  echo "<td align='center'><b>Home</b></td>";
 }
 //If $section == profile, make the text 'Profile' unclickable else make it into a clickable hyperlink
 if ($section != "profile"){
  echo "<td align='center'><a href='profile.php'>Profile</a></td>";
 }else{
  echo "<td align='center'><b>Profile</b></td>";
 }
 //If $section == product, make the text 'Product' unclickable else make it into a clickable hyperlink
 if ($section != "product"){
  echo "<td align='center'><a href='product.php'>Product</a></td>";
 }else{
  echo "<td align='center'><b>Product</b></td>";
 }
 //If $section == contact, make the text 'Contact Us' unclickable else make it into a clickable hyperlink
 if ($section != "contact"){
  echo "<td align='center'><a href='contact.php'>Contact Us</a></td>";
 }else{
  echo "<td align='center'><b>Contact Us</b></td>";
 }
 echo "</tr></table>";
 echo "<br>";
?>

If you look at "header.php", it's using a conditional statement to display the respective contents into "home.php" and "contact.php". Hence by reusing the "include()" function in PHP, you can save a lot of time copying and pasting the header and the footer of the website and probably various components or layout that you are going to use over and over again.

Note: this is also a useful way to store some variables that you might be using to make a connection to a mySQL server.

Click here for the example that I had created for the post.
Click here for the source codes of the example that I had created.

No comments:

Post a Comment