1 00:00:00,370 --> 00:00:04,270 You've learned a lot about programming in this course variables, operators, and 2 00:00:04,270 --> 00:00:05,430 conditionals. 3 00:00:05,430 --> 00:00:08,650 You've also learned how to make PHP work on a web page. 4 00:00:08,650 --> 00:00:11,630 The last thing we're going to do is add the unit converter and 5 00:00:11,630 --> 00:00:14,750 the daily exercise program to this web page. 6 00:00:14,750 --> 00:00:17,020 We could copy the code from each of these files and 7 00:00:17,020 --> 00:00:21,550 paste it into our HTML just like the other code blocks on our web page. 8 00:00:21,550 --> 00:00:22,880 We don't have a lot of code now. 9 00:00:22,880 --> 00:00:25,080 But as you write more complex applications, 10 00:00:25,080 --> 00:00:28,400 you'll be writing many many lines of code. 11 00:00:28,400 --> 00:00:31,590 When that happens, splitting up your code into multiple files 12 00:00:31,590 --> 00:00:35,480 makes it easier to find, read and work with a specific section of code. 13 00:00:36,550 --> 00:00:40,035 Just like linking to a CSS, file instead of having all our styles inline 14 00:00:40,035 --> 00:00:43,550 PHP allows us to include other files so 15 00:00:43,550 --> 00:00:47,060 they function, as part of this page, while keeping them separate. 16 00:00:47,060 --> 00:00:48,310 So let's jump back to our code. 17 00:00:49,980 --> 00:00:53,130 Before we start including our other page piece scripts. 18 00:00:53,130 --> 00:00:55,280 Let's clean up our file structure. 19 00:00:55,280 --> 00:00:57,280 To keep our files organized and 20 00:00:57,280 --> 00:01:00,220 know at a glance where to find the files we need to work with. 21 00:01:00,220 --> 00:01:02,660 It's a good idea to have your main directory 22 00:01:02,660 --> 00:01:06,330 only contain the actual pages that will show on a site. 23 00:01:06,330 --> 00:01:11,280 All other files should be sorted into subfolders, such as CSS and IMG. 24 00:01:12,370 --> 00:01:17,040 Typically files we are including are stored in a folder named inc, 25 00:01:17,040 --> 00:01:17,950 short for include. 26 00:01:17,950 --> 00:01:22,514 Let's create an inc folder and move our other PHP files into this folder. 27 00:01:30,135 --> 00:01:34,201 The only file we should have in the main directory is index.php. 28 00:01:41,320 --> 00:01:43,850 Now, let's include the unit converter on our webpage. 29 00:01:45,280 --> 00:01:49,010 An include works as if I had pasted the code exactly where I placed 30 00:01:49,010 --> 00:01:50,110 the include code. 31 00:01:50,110 --> 00:01:54,370 I already have a header on the page that says Unit Conversion. 32 00:01:56,110 --> 00:02:00,480 Let's include our script below that line. As always, 33 00:02:00,480 --> 00:02:03,010 start by opening and closing the PHP code block. 34 00:02:04,410 --> 00:02:07,460 Within the code block we use the keyword include, 35 00:02:08,530 --> 00:02:12,540 followed by the file we wish to include surrounded in quotes. 36 00:02:12,540 --> 00:02:16,560 Since our file's within a folder, we need to reference that folder as well so 37 00:02:16,560 --> 00:02:20,330 we use inc/units. 38 00:02:20,330 --> 00:02:23,780 Without PHP, and we end our statement with a semicolon. 39 00:02:25,560 --> 00:02:27,440 Let's save this page and refresh the browser. 40 00:02:29,310 --> 00:02:33,550 Great, our conversion displays exactly where we place the include. 41 00:02:33,550 --> 00:02:36,540 The problem is that the formatting is not very good. 42 00:02:36,540 --> 00:02:39,660 It's all smushed together and hard to read. 43 00:02:39,660 --> 00:02:44,820 When we display a text on a website we need to use HTML tags for formatting. 44 00:02:44,820 --> 00:02:48,790 Let's edit our units.php file and add those tags. 45 00:02:48,790 --> 00:02:51,826 We'll add HTML paragraph tags around the weight. 46 00:02:58,581 --> 00:03:01,900 Then we'll add separate paragraph tags around the distance. 47 00:03:06,120 --> 00:03:08,720 Now we can save our page and refresh the browser. 48 00:03:11,490 --> 00:03:12,780 This time we see the weight and 49 00:03:12,780 --> 00:03:16,620 distance on separate lines making it much easier to read. 50 00:03:16,620 --> 00:03:18,360 One down, one to go. 51 00:03:18,360 --> 00:03:20,580 Let's add the daily exercise to this page as well. 52 00:03:21,760 --> 00:03:23,600 I have another header called daily exercise. 53 00:03:25,020 --> 00:03:29,180 Let's include our exercise.php file below that header. 54 00:03:29,180 --> 00:03:30,831 Open and close another PHP block. 55 00:03:32,985 --> 00:03:36,597 And within that block we use the include keyword again. 56 00:03:38,464 --> 00:03:43,418 And this time we'll use inc/exercise.php and 57 00:03:43,418 --> 00:03:46,570 end the line with a semicolon. 58 00:03:47,680 --> 00:03:49,530 Let's save the page and refresh the browser. 59 00:03:52,210 --> 00:03:53,090 Great job. 60 00:03:53,090 --> 00:03:57,240 We now have our unit conversion and our daily exercise listed on our Web page.