Locked lesson.
About this lesson
How to include things from other files in your PHP program.
Exercise files
Download this lesson’s related exercise files.
Include Function.docx59 KB Include Function - Solution.docx
59 KB
Quick reference
Include Function
How to include things from other files with the Include function.
When to use
Any time you want to include something from another file, you might use the Include function.
Instructions
To use the include function:
include 'filename.php';
If there's a problem, include will give you a warning error message, it won't crash completely.
Hints & tips
- Include things from other files with the include function.
- 00:05 Okay, in this video I wanna talk about the include function.
- 00:07 Now, it's possible to include other files in your PHP file.
- 00:12 So there's a lot of reasons why you might wanna do this.
- 00:15 These days, if you go to any website,
- 00:16 they generally have a header at the to with a nav bar,
- 00:20 they have a footer at the bottom with links and copyright information.
- 00:24 They have menus sometimes on the left or the right side, and a lot of times,
- 00:28 you wanna create these dynamically, and
- 00:29 you wanna pull them from other files just a content management type of thing.
- 00:34 And we'll do this a little bit later on when we start templating out a website,
- 00:37 but one of the ways to do that is to use the include function.
- 00:40 So I'm gonna come over here and right click on our directory thing here and
- 00:44 click new file, and we're just gonna call this footer.php.
- 00:49 So let's open this footer.php file and let's just create echo and then say,
- 00:55 Copyright C and then the date.
- 01:00 Let's put space in there.
- 01:02 And we use our date function and let's say we want the year.
- 01:06 So if we save this and come back here to our index PHP file,
- 01:10 now let's say we wanna include this footer on our index page.
- 01:14 We would just use the include function which is include,
- 01:17 and most functions we use the parentheses.
- 01:20 We actually don't for this.
- 01:21 We just do this and go footer.php.
- 01:25 Now if we save this, come back here and hit reload, copyright c 2017.
- 01:30 That's really, really cool and really easy to do.
- 01:34 Now say, if you had like a website with 20 pages on it and
- 01:37 you wanted to include this footer on each of those pages,
- 01:41 you could just add this line of php code to the the bottom of each page.
- 01:45 And then later on, if you wanted to change this,
- 01:49 if you wanted to change this to copyright c,
- 01:53 John Elder and then All Rights Reserved.
- 01:57 Save this and when you come back to your main page and hit reload,
- 02:00 it automatically updates.
- 02:02 Now, if you had 20 pages or 50 pages or a thousand pages of your website,
- 02:06 this would update on every single one of those pages automatically and immediately.
- 02:11 And so that's really, really important for templating out websites.
- 02:14 So this is the include function.
- 02:16 One thing to note if say we change the name of this to footer 2, right, but here
- 02:21 on our index file, we're still calling footer.php, but there is no footer.php.
- 02:26 So what happens?
- 02:27 Well, if I click reload, boom.
- 02:28 We get this warning.
- 02:29 Now, this is just a warning.
- 02:31 The thing still executed.
- 02:32 In fact, we can see that by, let me type in all the things.
- 02:39 Now, let's do another line break.
- 02:42 We save this and hit reload.
- 02:44 It executes this but then we get a warning.
- 02:47 And in fact, we can copy this below, save it again, hit reload.
- 02:53 And her we have all the things.
- 02:54 So even though it couldn't find the file, the script still ran.
- 02:58 We just got a warning.
- 02:59 And that's important cuz a lot of times in PHP or other programming languages,
- 03:03 if you get an error, the whole thing crushes and stops.
- 03:06 But with include, you just get a warning and that's useful to know and
- 03:09 you'll see why in a little bit.
- 03:11 That's the include function, very useful.
- 03:13 In the next video, we'll talk about the require function.
Lesson notes are only available for subscribers.