1 00:00:00,560 --> 00:00:04,550 Now that we have our errors showing we're ready to get started actually 2 00:00:04,550 --> 00:00:06,700 addressing those errors. 3 00:00:06,700 --> 00:00:09,320 Many languages throw an exception 4 00:00:09,320 --> 00:00:12,220 whenever there is any sort of problem with the code. 5 00:00:12,220 --> 00:00:15,360 These languages are what I would call exception heavy. 6 00:00:16,890 --> 00:00:19,570 PHP is an exception light language. 7 00:00:19,570 --> 00:00:24,110 In most cases PHP will output an error instead. 8 00:00:24,110 --> 00:00:28,490 Like most things in PHP, you do have quite a bit of control over which things 9 00:00:28,490 --> 00:00:33,250 cause exceptions and other advanced options handling errors. 10 00:00:33,250 --> 00:00:35,390 Check out the teacher’s notes for more information. 11 00:00:37,060 --> 00:00:40,550 Errors can be output to the screen, local disk or both. 12 00:00:41,590 --> 00:00:48,100 PHP can also choose which error severities it will report and which it will ignore. 13 00:00:48,100 --> 00:00:53,320 The most common errors are E_NOTICE, E_WARNING, and E_ERROR. 14 00:00:53,320 --> 00:00:58,015 After throwing a notice or a warning, PHP will keep executing the script. 15 00:00:58,015 --> 00:01:04,900 E_ERROR or fatal errors on the other hand halt the script in an unrecoverable way. 16 00:01:04,900 --> 00:01:09,470 It can be tempting to ignore notices or 17 00:01:09,470 --> 00:01:13,440 warnings since they don't really stop things from working. 18 00:01:13,440 --> 00:01:18,370 But catching and fixing those things from the start is much better practice. 19 00:01:18,370 --> 00:01:21,640 In the long run, it's better to do things properly. 20 00:01:21,640 --> 00:01:25,560 For example, if a feature is marked as deprecated, 21 00:01:25,560 --> 00:01:29,270 you probably wanna know about it before it's removed. 22 00:01:29,270 --> 00:01:32,750 Otherwise, an upgrade becomes much harder later on. 23 00:01:34,000 --> 00:01:36,550 Let's take a look at errors4. 24 00:01:36,550 --> 00:01:38,630 This file is completely full of broken stuff. 25 00:01:39,890 --> 00:01:43,940 On line 3 we can see the error reporting function being used. 26 00:01:43,940 --> 00:01:49,060 This function is how we control which severity or errors will be reported. 27 00:01:49,060 --> 00:01:53,078 And this code specifically requests that only E_ERROR, E_WARNING and 28 00:01:53,078 --> 00:01:54,563 E_PARSE are reported. 29 00:01:55,720 --> 00:01:58,960 An error being reported means it can go into a log file. 30 00:01:58,960 --> 00:02:02,210 And maybe be displayed down the screen depending on the settings. 31 00:02:03,350 --> 00:02:08,680 We can see in line 5 that the display errors are turned on. 32 00:02:08,680 --> 00:02:11,220 So if we preview this script in a browser, 33 00:02:11,220 --> 00:02:14,670 we'll be able to see our errors because we have display errors turned on. 34 00:02:18,080 --> 00:02:22,330 Here we see int(5), so some code seems to be working. 35 00:02:22,330 --> 00:02:26,140 And we see some errors but they're all jammed up on the same line. 36 00:02:26,140 --> 00:02:29,580 This is because it's not outputting errors as html. 37 00:02:29,580 --> 00:02:31,110 It's outputting them with just text. 38 00:02:32,630 --> 00:02:35,037 So let's go back and turn on html formatting. 39 00:02:41,986 --> 00:02:43,660 Much better. 40 00:02:43,660 --> 00:02:46,580 Now what's really strange is that I know from the code that I've written 41 00:02:46,580 --> 00:02:50,630 that there should be four errors because I intentionally broke a lot of stuff. 42 00:02:50,630 --> 00:02:52,990 Let's take a look at our code and see what could be wrong. 43 00:02:54,880 --> 00:03:00,270 The first thing you might notice is that 5 is being added to the variable called nope 44 00:03:00,270 --> 00:03:03,970 and nowhere in this code is nope being defined. 45 00:03:03,970 --> 00:03:07,440 That should throw a notice but we aren't seeing one. 46 00:03:07,440 --> 00:03:11,820 This is because we have not requested PHP to report notices. 47 00:03:11,820 --> 00:03:13,070 And as such, 48 00:03:13,070 --> 00:03:17,410 an uninitialized variable will be treated as null without any complaints. 49 00:03:17,410 --> 00:03:22,530 5 plus null is 5, kinda. 50 00:03:22,530 --> 00:03:25,930 But it's not great to have this sort of code in our application. 51 00:03:25,930 --> 00:03:29,279 We don't want undefined variables floating around without any errors. 52 00:03:30,540 --> 00:03:34,200 Some developers pick and choose specific error levels like this. 53 00:03:34,200 --> 00:03:38,050 But that means that sometimes something might be forgotten. 54 00:03:38,050 --> 00:03:42,817 It also means that newly added error severities like E_DEPRECATED and 55 00:03:42,817 --> 00:03:45,040 E_USERDEPRECATED would be missed. 56 00:03:46,280 --> 00:03:50,120 Picking specific error levels unless you really know what you're doing 57 00:03:50,120 --> 00:03:51,600 is dangerous. 58 00:03:51,600 --> 00:03:55,140 The general advice of many is to turn the error reporting level up 59 00:03:55,140 --> 00:03:58,730 as high as you can using E_ALL. 60 00:03:58,730 --> 00:04:00,410 That's really easy to change. 61 00:04:00,410 --> 00:04:04,699 You just remove all these specific levels and change it to E_ALL. 62 00:04:09,580 --> 00:04:11,895 Now we can save that and preview it in the browser. 63 00:04:14,103 --> 00:04:15,250 Great. 64 00:04:15,250 --> 00:04:17,730 These are the four errors I'm expecting. 65 00:04:17,730 --> 00:04:21,860 You can see that there is a notice of an undefined variable, nope. 66 00:04:21,860 --> 00:04:25,070 It outputs 5 because it will still do the math. 67 00:04:25,070 --> 00:04:26,190 It will just warn you about it. 68 00:04:27,640 --> 00:04:33,304 Next we see a warning, creating a default object from an empty value on line 15. 69 00:04:34,550 --> 00:04:40,960 Next, we have a non static method, foobar, should be called statically on line 22. 70 00:04:40,960 --> 00:04:46,900 And finally, a fatal error, a call to an undefined method, foo, $nope. 71 00:04:46,900 --> 00:04:49,637 Let's see if we can fix these errors. 72 00:04:51,422 --> 00:04:55,260 So first, $nope equals 0. 73 00:04:57,020 --> 00:04:58,280 That should fix that notice. 74 00:05:00,350 --> 00:05:02,383 Then we wanna define our object. 75 00:05:06,169 --> 00:05:08,228 We'll set this to the standard class. 76 00:05:12,740 --> 00:05:14,520 Public function bar. 77 00:05:14,520 --> 00:05:20,086 This needs to be a static function to fix that error. 78 00:05:20,086 --> 00:05:23,123 And then finally, to get rid of the final fatal error, 79 00:05:23,123 --> 00:05:26,238 we need to make another static function called $nope. 80 00:05:34,951 --> 00:05:36,433 Let's see if this has worked. 81 00:05:38,801 --> 00:05:39,460 Perfect. 82 00:05:39,460 --> 00:05:41,550 The impossible happened. 83 00:05:41,550 --> 00:05:46,070 We got to the end of the file because we fixed all the errors thanks to reporting. 84 00:05:47,870 --> 00:05:50,650 One thing to remember is that in production 85 00:05:50,650 --> 00:05:56,450 you never want your end user to see PHP system errors on the screen. 86 00:05:56,450 --> 00:06:00,355 Displaying error messages to your site visitors can disclose 87 00:06:00,355 --> 00:06:05,735 important information about your site's setup and cause a huge security risk. 88 00:06:05,735 --> 00:06:09,925 On production, you want to have display errors turned off. 89 00:06:09,925 --> 00:06:11,855 Next, let's talk about logging.