Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Combining Multiple Files with Includes3:58 with Alena Holligan
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'.
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.
You've learned a lot about programming in this course variables, operators, and 0:00 conditionals. 0:04 You've also learned how to make PHP work on a web page. 0:05 The last thing we're going to do is add the unit converter and 0:08 the daily exercise program to this web page. 0:11 We could copy the code from each of these files and 0:14 paste it into our HTML just like the other code blocks on our web page. 0:17 We don't have a lot of code now. 0:21 But as you write more complex applications, 0:22 you'll be writing many many lines of code. 0:25 When that happens, splitting up your code into multiple files 0:28 makes it easier to find, read and work with a specific section of code. 0:31 Just like linking to a CSS, file instead of having all our styles inline 0:36 PHP allows us to include other files so 0:40 they function, as part of this page, while keeping them separate. 0:43 So let's jump back to our code. 0:47 Before we start including our other page piece scripts. 0:49 Let's clean up our file structure. 0:53 To keep our files organized and 0:55 know at a glance where to find the files we need to work with. 0:57 It's a good idea to have your main directory 1:00 only contain the actual pages that will show on a site. 1:02 All other files should be sorted into subfolders, such as CSS and IMG. 1:06 Typically files we are including are stored in a folder named inc, 1:12 short for include. 1:17 Let's create an inc folder and move our other PHP files into this folder. 1:17 The only file we should have in the main directory is index.php. 1:30 Now, let's include the unit converter on our webpage. 1:41 An include works as if I had pasted the code exactly where I placed 1:45 the include code. 1:49 I already have a header on the page that says Unit Conversion. 1:50 Let's include our script below that line. As always, 1:56 start by opening and closing the PHP code block. 2:00 Within the code block we use the keyword include, 2:04 followed by the file we wish to include surrounded in quotes. 2:08 Since our file's within a folder, we need to reference that folder as well so 2:12 we use inc/units. 2:16 Without PHP, and we end our statement with a semicolon. 2:20 Let's save this page and refresh the browser. 2:25 Great, our conversion displays exactly where we place the include. 2:29 The problem is that the formatting is not very good. 2:33 It's all smushed together and hard to read. 2:36 When we display a text on a website we need to use HTML tags for formatting. 2:39 Let's edit our units.php file and add those tags. 2:44 We'll add HTML paragraph tags around the weight. 2:48 Then we'll add separate paragraph tags around the distance. 2:58 Now we can save our page and refresh the browser. 3:06 This time we see the weight and 3:11 distance on separate lines making it much easier to read. 3:12 One down, one to go. 3:16 Let's add the daily exercise to this page as well. 3:18 I have another header called daily exercise. 3:21 Let's include our exercise.php file below that header. 3:25 Open and close another PHP block. 3:29 And within that block we use the include keyword again. 3:32 And this time we'll use inc/exercise.php and 3:38 end the line with a semicolon. 3:43 Let's save the page and refresh the browser. 3:47 Great job. 3:52 We now have our unit conversion and our daily exercise listed on our Web page. 3:53
You need to sign up for Treehouse in order to download course files.Sign up