Foreword: The include statement built into PHP is going to save web developers a heap of time in making their web application. What we’re going to see in this tutorial is how we use the include statement to duplicate repetitive tasks, clean up code, and overall expand our knowledge on the PHP language. Onward!
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:
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
<?php 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
<?php 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
<?php include("http://www.YourUrl.com/includes/header.php"); // Easy File Inclusion ?>
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
<html> <title>A Basic PHP Website Using Include</title> <p style="text-align: center;padding: 10px;"> <a href="#">Home</a> <a href="#">Subpage 1</a> <a href="#">Subpage 2</a> </p> <p style="text-align: center;border: 1px dotted blue;">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.</p> </body> </html>
Now create a file called “header.php” in another window. We are going to take out some of the contents of the index.php file and replace it with our include statement. Our header.php file should look like the following:
Contents Of header.php – Copy And Paste
<?php <a href="#">Home</a> <a href="#">Subpage 1</a> <a href="#">Subpage 2</a> ?>
Now we have two separate files. But we have a problem- how do we get the URLs from the header.php file into the index.php file? Simple! Review the code below to see where we put the include statement:
<html> <title>A Basic PHP Website Using Include</title> <body> <p style="text-align: center;padding: 10px;"> include("header.php"); ?> </p> <p style="text-align: center;border: 1px dotted blue;"> 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. </p> </body> </html>
Now upload both files to a same directory and test it out! If you’d like, you can change around the contents of the header.php file, upload it to your web server, and see the changes in real-time. This may not seem like a big time saver with only one page to use it on, but if we were to have a website filled with 100 pages, this would be quite the little time saver!
Variable scope sounds like a mean phrase to anyone who doesn’t know what it is. The scope of a variable is just how far it “reaches”- or basically where it can and can’t be accessed. Variable scope can be seen when looking at two separate PHP scripts. If they are not connected in any way, then the variables of the first script will not interfere with variables in the second. (And vice versa)
We deal with scope in the Include construct quite simply because we are merging two files- so if we are using the same variable name in both scripts, which one gets precedence?
In our previous example we are not dealing with scope since we aren’t using any variables. But we can change that with the following edits to both index.php and header.php:
header.php – Edits To Show Variable Scope
<?php $home = "<a href='#'>Home<a>"; $sub1 = "<a href='#'>Subpage 1<a>"; $sub2 = "<a href='#'>Subpage 2<a>"; ?>
We just declared our three variables as our links, so that we may see which variables take precedence in our index.php file. Our index.php file should be updated as seen below:
Contents Of index.php
<html> <title>A Basic PHP Website Using Include</title> <body> <p style="text-align: center;padding: 10px;"> <?php $home = ""; $sub1 = ""; $sub2 = ""; include("header.php"); echo "$home $sub1 $sub2"; ?> </p> <p style="text-align: center;border: 1px dotted blue;">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.</p> </body> </html>
So what do you think the script will output to the browser? As you can see, we declared each variable as an empty string before we included the file. But in the include of the file, we stated that the variables should contain links.
Upon testing this script, one can see that the output is going to be successful in displaying the correct navigation links. From this example it is seen that an included file will indeed overwrite variable values unless otherwise coded.
It should also be made clear that an include file is only going to have access to variables and functions that have been declared before the line used to include the file. In the following example we will declare a variable and reference it within the include file. (No need to test this out unless you want- you can easily see the results with the two short snippets below)
index.php – Testing Variable Scope
<?php $hello = "hello world!"; include("echo.php"); ?>
Now for the echo.php file:
echo.php – Testing Variable Scope
<?php echo $hello; ?>
Once this script is run, we will see that “hello world!” is output to the screen. If we were to put the variable after the include line, we wouldn’t get any results at all.
PHP is considered a generally secure language since it hides the contents of the PHP code from the user. (Go ahead and view the source of the HTML of pages we have created- you won’t see PHP code!) If we take this protection away, anyone can see the contents of our scripts. Luckily you have to make the mistake of not using the PHP extension for this to happen.
It is sometimes common to see some use plain text or even a .INC file type for their includes. If you use the wrong extension or remove the PHP tags from the header.php file we were working with, you’ll notice that the plain text is output to the screen if you try to access header.php directly. This goes to show that when we use includes, always remember to use the PHP tags to enclose the data if it is using any type of sensitive data. (Or any time at all, since it builds good programming practice!)
The second major security concern is including files from a remote location. If we are only using local resources on our website, such as template files we have created, there is no reason to fret. But if we are pulling information from other websites, which is sometimes illegal in many cases anyway, the remote website can inject code into our websites and get confidential information with ease. As such, developers should only agree to use includes from remote resources if they are to be trusted without doubt.
As we get more familiar with the Include statement, we can get a little more skilled in the way we use it. From using Includes in functions or even using IF statements to selectively include correct files, there is much to expand on here.
after look for about 20 min for how to include files in other directories, finally found the answer on your page. Thanks for taking the time.
it would also make sense to make inclusion paths be the written directory path/location of the file, but i guess ‘../’ works too… if you’re a unix user.
Thanks for pointing out the security issues. That’s something I hadn’t really thought through before.
A Basic PHP Website Using Include
include(“header.php”); ?>
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.
I am not getting any good results for the “include” PHP code here and I can’t figure it out.
This is the PHP code I put into my index.php page:
I made the two PHP pages in Notepad exactly as it is above and opened it straight in my browser and the links do not show at all.
For something apparently so simple, I am spending way too much time on learning PHP.
I also, after a lot of research, downloaded XAMPP, but I haven’t touched it yet because for an absolute beginner with PHP, it is basically learning Japanese.
Yes, ../ is for a unix user. Try ..\\ if you feel more comfortable with DOS/Windows.