Locked lesson.
About this lesson
Similar to "Include", but used for a slightly different reason.
Exercise files
Download this lesson’s related exercise files.
Require Function.docx59 KB Require Function - Solution.docx
59.1 KB
Quick reference
Require Function
The require function is another way to add things from other files.
When to use
You would use the require function when an error message that crashes your whole program is needed.
Instructions
Require is another way to add things from one file to another.
require 'filename.php';
The main difference between it and "include" is that if there's a problem, you'll get an error message and then the entire program will stop.
Hints & tips
- require 'filename.php'
- If there's an error while using require, your whole program will stop.
- 00:04 In the last video, we talked about the include function.
- 00:07 In this video, I wanna talk about the require function.
- 00:10 And the require function is very similar to the include function.
- 00:13 In fact, they're basically identical.
- 00:15 You just require, save this, hit reload.
- 00:19 We get the same thing.
- 00:20 The main difference is with include, if there's a problem,
- 00:25 you get a warning and the script keeps going.
- 00:27 With require, it's required, and if it's not there, you don't get a warning,
- 00:32 the whole thing just shuts down.
- 00:33 So we can see, we named our file here footer.php,
- 00:36 and it just has this copyright statement in it.
- 00:39 And if we come up here and change the name of our footer file back to footer2,
- 00:43 you can see we've got all the things, the footer, and then all of the things.
- 00:48 So if we save this, come back here and hit reload,
- 00:50 that footer file is no longer there.
- 00:51 So we get all of the things, it tries to call footer2, it can't find it.
- 00:56 And instead of getting the bottom All the things..., we get nothing.
- 01:00 The program just stops.
- 01:02 So require, sometimes you want that.
- 01:04 So if you have a website that you really need something to come up on the website
- 01:08 and you want the whole thing to stop if it doesn't, use require and that will happen.
- 01:12 WordPress uses require, I believe.
- 01:14 Anyway, that's the main difference.
- 01:16 So what else can we do with this?
- 01:18 Let's come to our footer and let's rename this footer file back to footer.
- 01:22 Now let's create a variable called name and set it equal to John Elder.
- 01:31 We've required this file, and basically php is gonna pull this file in and
- 01:37 allow us to use it on our index page, right?
- 01:40 This is echoing something out, but it is also just setting a variable.
- 01:44 So we can access that variable wherever we want, here.
- 01:49 And do some more line breaks and let's create another php tag.
- 01:54 And let's just echo out that variable, we called it name, didn't we?
- 01:59 Yeah, name.
- 02:00 So if we save this file, come back here and hit reload, we have John Elder,
- 02:05 very interesting.
- 02:06 So here's something fun to try.
- 02:08 Take the php, take this line, and
- 02:10 let's put it up here above this require statement.
- 02:15 So if we save this, come back here and hit reload.
- 02:18 The thing disappears because with the flow of our program,
- 02:21 programming things start at the top and they come down the file.
- 02:25 So we're coming down to here, echoing out name.
- 02:27 Our program doesn't know what name is because we haven't added this file yet.
- 02:32 So this needs to come below this.
- 02:35 A common way to fix that might be to put this line at the very,
- 02:38 very top of your thing.
- 02:40 But if that happens, it's gonna echo out that at the top of the screen.
- 02:43 So that's just something to sort of keep in mind but that is require.
- 02:47 The main difference between require and include,
- 02:49 like I said, require will shut your program down if there's a problem,
- 02:53 include will just give you a little bit of a warning.
- 02:55 In the next video, we're gonna talk about forms and form handling.
Lesson notes are only available for subscribers.