A PHP "Include" is a file that is included into another file. This concept allows you to re-use content throughout your website.
For example, you could have a standard header and footer on every page of your website. Instead of copying/pasting this code on to every page, you could simply place your header code into one include file and your footer code into another include file. Then, on every page, all you need to do is refer to the header and footer include files. You can do this either using the
include()
function or the require()
function.
There's only a small difference between the include function and the require function. More on that below.
The include()
Function
To include a file using the
include()
function, you simply call the function (as you would any other function) and insert the file path as a parameter.Usage Example
include("header.php");
echo "Here's the rest of the page.";
include("footer.php");
?>
The require()
Function
Usage of the
require()
function is exactly the same as the include()
function - simply call the function and pass the path of the include file.
The difference between the
include()
and require()
functions is in the way they handle errors. If the included file can't be located, the include()
function will still display the rest of the page (as well as an error). The require()
function, on the other hand, will simply display an error - it won't display the rest of the page.Usage Example
require("header.php");
echo "Here's the rest of the page.";
require("footer.php");
?>
No comments:
Post a Comment
Hey, It's Been Grabbed