include, require, require once PDF Print E-mail
User Rating: / 0
PoorBest 
PHP - Basics
Written by Stuart Duncan   
Wednesday, 14 October 2009 14:46

Have you ever wished that you could just make one header, with a logo and menu and just have it be in one file and just include that file in all of your HTML files? So that when you make a change to the menu, you can do it just one time instead of hundreds of times? Imagine a typo, and having to fix that in all of your HTML files. Frustrating!

PHP's answer to this comes in the form of includes and requires. There are a few ways they can be used and couple subtle differences.

 

 

Include

This is the most commonly used feature of PHP, especially with newer scripters out there... it's an extremely handy way to do common tasks one time and using them over and over again.

The idea is that you can make a part of a file, like a header or menu, and just include it into another file.

Let's say that you are tired of writing out the html header over and over, including the css and javascript and maybe the top logo, you could just make a header.php file with all of that, and then include that into the rest of your files.

This is what the header would look like:

<html>
<head>
<title>My Site Name</title>
</head>
</head>
<body>
<img src="/logo.gif">

Save this as header.php.

Then you would include this into the rest of your files, for example:

<?php
include 'header.php';
?>
<p>Welcome to our site. This is the main page.</p>
</body>
</html>

Save this as index.php.

Now when you load index.php into your browser, it will go and fetch the header.php to place into the index. And you can do it for your contact, about, login, register and every other page you could possibly have.

It's really quite that simple. But to mix things up a bit, I will show you a couple of other ways to do the same thing:

 

Require

This command does essentially the same thing as include except that PHP gives it a high level of fail. That is to say, if include is unable to find the file, it will just not include it and continue on showing the rest of the page. But if require fails, it will throw a fatal error and stop.

To give you an idea of just how similar it is, here it is in the example above:

<?php
require 'header.php';
?>
<p>Welcome to our site. This is the main page.</p>
</body>
</html>

 

Include_once, Require_once

These commands work and react exactly as the previous commands except for one exception... if you try to include (or require) the same file more than once, it will only do it the first time. This would do us no good if we wanted a menu to be in multiple places on a page, but let's say that you have some PHP variables being set but may be included depending on various other settings on a page (for now, with just includes and variables, it's not likely but further into development, you may find that some files can be included in different places but really shouldn't be, these commands would come in handy).

 

What more can you do?

Well, let's say that you have 2 different headers that you'd like to include. For now, I haven't covered if/else statements but let's say that you have one user who is logged in and one who isn't. You may have 1 header file for each since someone who is logged in will need a logout link, and someone who isn't will need a login link.

What you could do is something like this:

<?php
if( $loggedin == 'yes' ){
$header = 'header_user.php';
}
else if ( $loggedin == 'no' ){
$header = 'header_guest.php';
}
include $header;
?>

Hopefully you can follow along enough to see that someone who is logged in should be able to see the header_user.php file while someone that is not should see the header_guest.php file. This gives you a lot of freedom, if you should want to make a change to the headers that users see, you just change that one file and ALL of the pages that users will see will automatically be updated. No more running through hundreds of HTML pages just to change a link.

Hopefully this helps give you some ideas on how to streamline and make your work more productive.

 

 

Add comment


Security code
Refresh

Login to TyCamTech.com

Donate

Must have Pepsi, feed my addiction here:


Recommended Books

Introduction to Programming
A Quick And Easy Ebook That Introduces Readers To Computer Programming In A Jargon Free, Easy To Understand Way.

Learn PHP in 17 hours!
If you can copy and paste into HTML and you can follow simple instructions...

How To Flip Websites Home Study Course.
Complete Video Instruction On How To Quickly Set And And Flip (i.e. Sell) Websites For Profit. 6 X Video Lessons Plus Bonuses -- Great Value For Buyer And Maximum 75% Payout For Affiliate!

Follow us on



Follow tycamtech on Twitter