PHP Tutorial (& MySQL) #15 - Include & Require
Key Takeaways
Uses PHP include and require functions to import other PHP files
Full Transcript
alright then so now we've taken care of I don't want to say the boring parts of PHP but the basics rather of PHP we can start to move on and do some slightly more fun stuff and even start our very own ninja Pizza project that we saw in the very first lesson of this series so the first thing I want to show you a two built in PHP function is called include and require and they both do essentially the same thing but the way they react to errors is different and we'll see that shortly but what they do is essentially require or include like the the name of the function says another file into this file so what I've already done is created this other file called ninjas PHP right here and it's just a simple bit of code we've created a ninjas variable and that is an array with three elements and then we're echoing position one which is this right here from the array and a br tag okay so what I'd like to do now is include this file into this file right here so I could say include and then in brackets the path to that file in a string relative to this file so it's just ninjas PHP because we're in the same directory right here and if I do that then what it's going to do is go out and grab this code and port it into this file and run it here okay now I'm just gonna echo here and of PHP right and we should see this code first then this so I've saved that I'm going to refresh over here and you can see now we get Rio then end of PHP and we get Rio because like I said we've included this file into this file so when we run it it's echoing this thing right here which is right okay then we're echoing this so that is include it allows us to go out grab another file and include it in this file right here now the other function require do is pretty much the same thing so let's just require and then in brackets ninjas or PHP okay so save that and we should see two rows on the page now once it cool because it's doing exactly the same thing okay so I did say there is a difference in how they react to Eretz so if I now let me just copy this and pay it downhill then I'm going to change this to ninjas with two S's so obviously this file right here doesn't exist right so there should be some kind of error on the screen and remember we're still echoing this at the bottom now let me save that and refresh it so we do get an error right here because obviously this file doesn't exist but we still see end of PHP echoed to the screen so it's carried on with the rest of the PHP right now if we change this to require require this file which doesn't exist save it we should still get an error but notice now it's a fatal error and it's not going to carry on with the rest of the code okay so where is include carries on with the code if there's an error trying to grab this file require doesn't allow that it doesn't carry on with the code so that's the major difference there now you might also see these written like this without parentheses and that's absolutely fine that's going to work as well and we'll do the same with include so include ninjas dot PHP they both work as well it's just another way of writing this refresh and we see it's echoed twice okay so you'll probably see this kind of code using require or include in things like WordPress themes to load in different parts of a template and it's good because it allows us to modularize our code and reduces code repetition for example if we have a block of code that shows on every page of your website or several pages of your website we don't have to manually code it on each of those pages we can just code it once in an external file and then we can include or require it on each page that we want that code to appear on right then when we need to update the code we only have to do it once and it will update on all of those pages that includes or requires it so simple example down here I'm going to create now another file called content dot PHP and all I'm going to do in here is a div and then inside that div and h1 and we're gonna use some PHP here so PHP tab and then we're going to echo some content so imagine we want to add this content and now it's not great at the minute but to several different pages in the HTML template and what we could do down here is we could include that or require it so I'm just going to use include so I'll say PHP include and we're going to include the content I'll use parentheses content dot PHP okay so save that refresh and we can see some content right here now in the HTML now like I said we could do this multiple times so I could do it two or three times and refresh over here now if I want to update this I don't have to update it three times I just have to update it once and it's going to update all of these different instances where we include it and these could all be on different pages so that's why it's so powerful and so useful so we're going to be doing something similar to this and using the power of this in the very next tutorial where we're going to start our ninja Pizza project and create a navbar and our footer templates
Original Description
Hey gang, in this PHP tutorial I'll show you how to use two functions - include & require - to import other PHP files.
----------------------------------------
🐱💻 🐱💻 Course Links:
+ Course files - https://github.com/iamshaunjp/php-mysql-tutorial
+ VS Code editor - https://code.visualstudio.com/
+ Materialize Playlist - https://www.youtube.com/watch?v=gCZ3y6mQpW0&list=PL4cUxeGkcC9gGrbtvASEZSlFEYBnPkmff
🤑🤑 Donate
+ https://www.paypal.me/thenetninja
🎓🎓 Find me on Udemy
+ https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
🎓
Tutor Explanation
DeepCamp AI