Web Info and IT Lessons Blog...

Wednesday 28 January 2015

Include and Require Statements in PHP

The previous lesson in PHP Lessons Series was about GET and POST and in this lesson we will learn about Include and Require Statements in PHP.

Include and Require statements have a great importance in PHP. In PHP there are two types of include and two types of require statements.

1. include
2. include_once
3. require
4. require_once

The basic functionality of all of the above mentioned statements is same i.e to include an external file in our current document with a little differences between each statement. Each statement is discussed in detail below:

Include and Require in PHP

Include Statement:

Include statement is used to include the complete code of an external file in our current file in case we need the contents of the external file in our current file. For example, if we have an external file (calculations.php) containing the addition() function given below:


function addition($a,$b){
 $c = $a + $b;
 return $c;
}

The above addition function adds two variable values passed inside it and returns the result. Now we can access this function in our index.php file without having to write the function by including the file (calculations.php) in our index.php file.

The basic syntax of an include statement is given below:


include 'filename.php';

To include the above calculations.php file in our index.php file, just write the code given below at the top of your index file.

include 'calculations.php';

An external file is usually included at the top of index file to make sure that the external file is included before we use any of it's contents in our index file, otherwise we won't be able to use any of it's functions. Once an external file is successfully included in our index file, we can use all of it's contents in our index file.

What if file include fails?
If we try to include an external file in our index file which does not exist in our project, we will get a warning message and we won't be able to use any contents of the external file. If the include fails but we do not use any contents of the external file in our index file then we will get a warning message but the script of index file itself will run unaffected. For Example, if we echo something after the failed include file in our index.php file, the first thing we are gonna get after we run our index file is a warning message and then the text that is echoed.

Try this code:

include 'calculations1.php';

echo 'Success';

In the above code we have included the file calculations1.php in our index file which does not exist in our project so we will get a warning for that and then the text 'Success' will be echoed.

include_once Statement:

Just like include statement include_once is also used to include an external file in our current file but there is a small difference between the two. The basic syntax of an include_once statement is given below:


include_once 'filename.php';

Difference between include and include_once statements
The difference between include and include_once statement is that if we include a certain file using include statement in our index.php file more than once, it will be included more than once but if we include a certain file more then once using include_once, it won't be included more than once. In case of include_once statement a certain external file is included only once. For Example, if we have an external file (echo.php) with only this piece of code:


echo 'This file is included!';

and we include it in our index.php file more than once using include statement, we will see the text 'This file is included!' echoed multiple times. But if we do the same for include_once, we will see the text 'This file is included!' echoed only once.

So basically include_once is best to avoid multiple inclusions of the same files in our project.

Require Statement:

The functionality of require is also same as include i.e it is also used to include an external file but again there is a small difference.

The basic syntax of an require statement is given below:


require 'filename.php';

Difference between include and require
The main difference between include and require is that if the included file does not exist in our project, it will give us a warning but still the code after the failed include will run and give us some output but that's not the case in require. In require statement if we specify a file which does not exist in our project, no code after the require statement will run and our script will die there.

Require statement is useful in cases where we do not want to load anything in case a very important file fails to load.

require_once Statement:

Just like include_once, require_once is also used to avoid multiple loading of the same file in our project. That's the only difference between require and require_once in case you were wondering.

Related Posts
Session in PHP
Cookies in PHP
Get and Post methods in PHP
How to get the ip address of user in PHP?

No comments:

Post a Comment