 Hey everybody this is Brian and welcome to the 30th lamp tutorial. Today we're going to be discussing error handling. Okay one thing you'll see a lot in PHP scripts is called the die statement where you're saying die. The example they give here is if not file exists meaning the file does not exist then die file not found. All die does is well it kills PHP tells PHP stop processing just exits. You can give it a reason and that's actually considered bad form. I don't like doing that. The reason being is there's better ways of doing it and if you read the whole W3 schools or whatever you're using maybe a book you're reading you can create a custom error handler. Notice how there's an error level. Required specifies the report level must be a valid number. Good example of that would be what level do you really want this to be at and then there's an error message required. This is the actual error message you're spitting out and then the error file line and context and those are all optional and then here's different report levels like warnings notices etc etc etc. You read up all the stuff don't expect you to burn any of this in the memory and you can see here is a custom error handler right here we have an error number and an error string and you're just echoing it out and then calling die. To use that you would say set error handler and then the name of your function. We're gonna actually see how all this works they have an example here and I'm gonna write something pretty similar just so it's up to speed here and we're gonna say set error handler and just close my error handler. It sounds like a really funky name actually let's call it let's use their name custom handler custom error sorry. We'll scatter brain today it's Friday I just spent an hour standing in line at the post office had to get a passport not thrilled all right so now we're going to actually make a function we're gonna say function and notice how the name has to match up custom error custom error gonna give it a variable the error number in the error string and now we're just gonna you guessed it echo some stuff out well htmo goodness here and anybody anybody out there play Halo I got Halo 4 and oh man I've been loving that I got it on the Xbox so I'm not used to the controls it's actually kind of messing with me a little bit because I'm used to the PC version and we'll say error now and then of course we're gonna here a string so we're just gonna print this out there's our little web page down here notice how it's not printing anything because we haven't actually set an area yet if you're quick and you saw there was something down there earlier it's because I'd previously done this tutorial there's some weird setup with record my desktop it'll get to a certain point just stops recording the sound so I'll make a whole 10 minute video and then it just stops recording really frustrating we're gonna trigger an error for an example and we're just gonna say echo and say test now we're echoing out a variable that just simply doesn't exist we haven't actually created this yet so when we save and run but the error 8 undefined variable test what's going on here we're setting an error handler and we're saying use the custom error function which we just created and in this function we're echoing out some stuff now what you really should do is actually stop the execution of PHP at that point because you're no longer doing anything notice how that's not required you can actually run throw an error messaging just keep to keep on going now in the die statement you can also say a reason because I said so and notice how that also prints out on the screen to pretty simple pretty easy to use the reason why you would use a custom error handler not just the die statement is you're overriding the die statement let's say you want to add in some sort of logic like email an administrator or write the log or you know maybe store something on database you know the number of failed login attempt something along those lines and we're actually triggering an error there's a better way of doing this so let's actually trigger an error now why would you want to trigger an error well that's a good question you would want to trigger error if certain conditions happen in this case I'm just triggering it see this is my error now in a real-world application let's do this let's cut this and say I equals three if I is greater than five echo do something just some very simple logic here and instead of this is my error we're gonna say I is less than five now let's look at this really quickly why would you want to do something like this well we have a variable in this case we're saying three in this purely example we're saying if I is greater than five then do something this could be like log in the user you know if their username and password equals this then do that otherwise we want to trigger an error actually raise an error or in some languages it's called throwing an error in other words we want to create an error condition save this use of undefined constant I assumed I that's because I screwed up and didn't put that notice how our our air handler is working even as we screwed up there we go error 1024 I is less than five so you can see how we're actually triggering an error condition some things you might also want to do in your handler I mentioned this earlier is actually log something and by log something I mean you're gonna actually write out to the system log and you can also optionally email the administrator or whomever so we're gonna say error log and notice how we've got some things here message type destination extra headers well let's handle that air string first type we're just gonna say one because it's an error and now the destination the destination this is what this would technically be not technically typically be an email address don't use my email address for testing I will simply blackmail you and you will never hear response from me ever okay now for the extra headers if you paid attention during our email copy you can see you can add extra headers let's actually say no reply website dot com and run that now you notice how we have the same results actually refresh here same results but what happens in the background now is we're writing out to the log and in about 30 seconds my phone's gonna go bing you got mail and I'm gonna have this nice little message mail to my phone let's offer this tutorial I hope you found this educational and entertaining I realize we are just scratching the surface of what air handling can actually do but we're gonna see more of it in coming tutorials