Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Errors4:30 with Phil Sturgeon
Errors have various severity levels, the most common being
E_WARNING. Generally PHP will keep on executing, but some severities like
E_CORE_ERROR will halt the program in an unrecoverable way.
[MUSIC] 0:00 In many languages, 0:05 an exception will be thrown whenever there is any sort of problem. 0:05 These languages are what I would call exception-heavy. 0:09 PHP is an exception-light language, 0:12 which in most cases will output an arrow instead. 0:14 Arrows can be output to the screen, local disk or both. 0:17 PHP can also choose which arrow severities it will report and which it will ignore. 0:20 The most common errors you run into are E_ERROR, E_NOTICE, and E_WARNING. 0:25 Generally PHP will keep on executing despite throwing most types of errors. 0:30 Though some severities like E_ERROR or 0:34 E_CORE_ERROR will halt the program in an unrecoverable way. 0:36 Some of this might feel like PHP telling you to eat your greens. 0:39 Sure, it could let you get away with it but 0:43 PHP knows that in the long run it's better to do things properly. 0:44 For example, if a feature is marked as deprecated you probably want to 0:47 know about it before it's removed. 0:50 Otherwise an upgrade becomes much harder later on. 0:52 So here is the index.php file that's completely full of broken stuff. 0:54 On line four we can see the core error_reporting function being used. 0:58 This function is how we control which severity or errors will be reported. 1:03 And this code specifically requests that only E_ERROR, E_WARNING, and 1:07 E_PATHS are reported. 1:11 An error being reported means it will go into the log files and maybe be 1:14 displayed on the screen, depending on the display errors option on line seven here. 1:17 If we run this script in preview mode now we'll see two errors on 1:23 the screen because we have display errors turned on. 1:26 So, let's run that preview. 1:28 So here we see int5. 1:32 Some code seems to be working and we see some errors but 1:34 they're all jammed up on the same line. 1:36 This is because however this has been configured it's not 1:38 outputting hours of HTML. 1:42 It's outputting them with just text. 1:43 So if we view the source we can see them more clearly. 1:45 Now what's really strange is that I know from the code that I've written that 1:47 there should be four errors because I intentionally broke a lot of stuff. 1:50 Let's try taking a look at our code and see what could be wrong. 1:53 So the first thing you might notice is that 5 is being added to 1:56 a variable called nope and nowhere in this code is nope being defined. 2:00 That should throw a notice, but we aren't seeing one. 2:04 This is because we have not requested PHP to report notices and 2:06 as such, an uninitialized variable will be treated as null without any complaints. 2:10 5 plus null is 5, kinda. 2:14 But it's not great to have that sort of code in our application. 2:17 We don't want undefined variables roaming free without errors. 2:19 Some developers pick and 2:23 choose specific error levels like this, but it means that some might be forgotten. 2:24 And it also means that newly added error severities like E_DEPRECATED and 2:27 E_USER_DEPRECATED will be missed. 2:31 Picking specific levels unless you really know what you're doing is dangerous. 2:33 The general advice of many is to turn error_reporting up level up as high as 2:37 you can using E_ALL. 2:40 That's really easy to change. 2:42 You just remove all of these specific levels and 2:43 change it to E_ALL, and we can save that. 2:48 If we go back to our view-source of the preview, we now see a lot more errors. 2:51 These are the four I'm expecting. 2:57 We can see here there's a notice of an undefined variable nope. 2:58 It outputs 5 because it will still do the math, it will just warn you about it 3:01 using a notice as a warning about creating a default object for an empty value. 3:04 A strict standard error has happened, 3:08 non-static method foo bar should not be called statically. 3:10 And a fatal error, right at the end. 3:14 Call to undefined method Foo_nope(). 3:16 So let's see if we can fix this code really quickly. 3:18 So, nope equals 0. 3:23 That's one fixed. 3:26 Now we can fix this warning by creating $wrestler equals new stdclass. 3:31 That should fix that error. 3:39 Public function bar, so this needs to be a static function to fix that error. 3:43 And to get rid of the final error we need to make another function. 3:50 Call it nope, see if this has worked. 3:57 This should also be a static function. 4:02 Perfect. The impossible has happened. 4:06 We've got to the end, because we fixed all of our errors thanks to reporting. 4:08 One thing to remember is that you never want your end users to see 4:13 errors on the screen. 4:16 In production, you want to have display errors turned off. 4:18 If you have access to the servers, you can remove that any set line from your PHP, 4:21 and rely on the server to decide if errors should be shown or not. 4:24 Next, let's talk about exceptions. 4:28
You need to sign up for Treehouse in order to download course files.Sign up