Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Instead of writing all our code in a single file, PHP allows us to INCLUDE other files, so they function as part of the current page, while keeping them separate. This is very useful for organization and not repeating code for things such as footers and navigation.
How includes work
Included files act as if you had pasted the code directly into the file. Any variables that have already been defined in the main file such as display_name can be utilized and CHANGED. If the file cannot be included PHP will give an error, but continue to process the script. If you want to guarantee that the file is included before continuing on, you should use 'require'.
Documentation
include - includes and evaluates the specified file.
include_once - the only difference being that if the code from a file has already been included, it will not be included again, and include_once returns TRUE. As the name suggests, the file will be included just once.
require - is identical to include except upon failure it will also produce a fatal error. In other words, it will stop the script whereas include only emits a warning which allows the script to continue.
require_once - identical except PHP will check if the file has already been included, and if so, not include it.
-
0:00
You've learned a lot about programming in this course variables, operators, and
-
0:04
conditionals.
-
0:05
You've also learned how to make PHP work on a web page.
-
0:08
The last thing we're going to do is add the unit converter and
-
0:11
the daily exercise program to this web page.
-
0:14
We could copy the code from each of these files and
-
0:17
paste it into our HTML just like the other code blocks on our web page.
-
0:21
We don't have a lot of code now.
-
0:22
But as you write more complex applications,
-
0:25
you'll be writing many many lines of code.
-
0:28
When that happens, splitting up your code into multiple files
-
0:31
makes it easier to find, read and work with a specific section of code.
-
0:36
Just like linking to a CSS, file instead of having all our styles inline
-
0:40
PHP allows us to include other files so
-
0:43
they function, as part of this page, while keeping them separate.
-
0:47
So let's jump back to our code.
-
0:49
Before we start including our other page piece scripts.
-
0:53
Let's clean up our file structure.
-
0:55
To keep our files organized and
-
0:57
know at a glance where to find the files we need to work with.
-
1:00
It's a good idea to have your main directory
-
1:02
only contain the actual pages that will show on a site.
-
1:06
All other files should be sorted into subfolders, such as CSS and IMG.
-
1:12
Typically files we are including are stored in a folder named inc,
-
1:17
short for include.
-
1:17
Let's create an inc folder and move our other PHP files into this folder.
-
1:30
The only file we should have in the main directory is index.php.
-
1:41
Now, let's include the unit converter on our webpage.
-
1:45
An include works as if I had pasted the code exactly where I placed
-
1:49
the include code.
-
1:50
I already have a header on the page that says Unit Conversion.
-
1:56
Let's include our script below that line. As always,
-
2:00
start by opening and closing the PHP code block.
-
2:04
Within the code block we use the keyword include,
-
2:08
followed by the file we wish to include surrounded in quotes.
-
2:12
Since our file's within a folder, we need to reference that folder as well so
-
2:16
we use inc/units.
-
2:20
Without PHP, and we end our statement with a semicolon.
-
2:25
Let's save this page and refresh the browser.
-
2:29
Great, our conversion displays exactly where we place the include.
-
2:33
The problem is that the formatting is not very good.
-
2:36
It's all smushed together and hard to read.
-
2:39
When we display a text on a website we need to use HTML tags for formatting.
-
2:44
Let's edit our units.php file and add those tags.
-
2:48
We'll add HTML paragraph tags around the weight.
-
2:58
Then we'll add separate paragraph tags around the distance.
-
3:06
Now we can save our page and refresh the browser.
-
3:11
This time we see the weight and
-
3:12
distance on separate lines making it much easier to read.
-
3:16
One down, one to go.
-
3:18
Let's add the daily exercise to this page as well.
-
3:21
I have another header called daily exercise.
-
3:25
Let's include our exercise.php file below that header.
-
3:29
Open and close another PHP block.
-
3:32
And within that block we use the include keyword again.
-
3:38
And this time we'll use inc/exercise.php and
-
3:43
end the line with a semicolon.
-
3:47
Let's save the page and refresh the browser.
-
3:52
Great job.
-
3:53
We now have our unit conversion and our daily exercise listed on our Web page.
You need to sign up for Treehouse in order to download course files.
Sign up