It should be noted that, much like the Echo statement in PHP, Include is not considered an actual function. Although it would behave like a function, we call it a language construct since it is “built into” PHP. We consider it an integral part of the language much like the IF statement! (You may notice some language constructs listed as functions- but this is only to improve documentation.)
The primary usage of this language construct in particular is to retrieve a remote file for inclusion into the currently running script. We are going to use the Include construct for three primary reasons:
1. Readability – We use the Include construct for replacing many lines of code with but one line of code. This increases the readability of an application, and ultimately lets us troubleshoot bugs quicker than ever before. We use the Include construct for replacing many lines of code with but one line of code. This increases the readability of an application, and ultimately lets us troubleshoot bugs quicker than ever before.
2. Save Time – We save time by using the Include() construct in a very ingenious way. If we were to change a link in our navigation menu, we would have to update that same link hundreds of time on different subpages. But if we used an Include() construct, we could just edit one file and the results on other pages would be instant!
3. Reliability – If an included file doesn’t exist or has malfunctioned, the server will tell us quite promptly. This is superb for troubleshooting! Error reporting is sometimes seen as a security risk, however, since it commonly shows semi-confidential information in the error.
PHP Include Tutorial – Examples Of Including A File
Before we can do dabble with an example, we need to learn how the Include syntax works. Using the Include construct is actually quite easy; we just need to know the URL of the code to be input, wrap it in parenthesis, and add the include statement.
An Example Of The PHP Include Syntax
include("header.php");
include 'footer.php';
// Each Way Is Correct, But The First Example Is More Readable
?>
In the above example we are calling to two different files, in which both are located in the same directory as the page being viewed. Both statements will work, but we personally like the first example since we view it as more readable.
If the file we wanted to include is in the parent directory, we simply use the “../” notation as seen below.
Including A PHP File From A Parent Directory
include("../header.php");
// Include A File From Parent Directory
?>
Note that every time we use the “../” notation we go up one level. But things don’t have to get complicated! We can simplify things by simply using the entire URL as seen below:
An Easier Way To Include A PHP File
include("http://www.YourUrl.com/includes/header.php");
// Easy File Inclusion
?>
PHP Include Tutorial – An Include Example
To get started we are going to need at least two files. First, we will take an already-made index.php file as seen below:
Contents Of index.php – Copy And Paste
Welcome to our website! The links above are being used in includes- saving us time that can be best used for developing better features for this website.
