PHP Error Handling & Error Handlers - Full PHP 8 Tutorial

preview_player
Показать описание
In this PHP tutorial, you will learn different types of errors in PHP as well as the basics of error handling. You will also learn how to trigger errors manually. This video is just an introduction to error handling to prepare you for a more advanced & in-depth video that will be coming out about error handling that includes exceptions in the second section of this course.

SOME OF THE WAYS YOU CAN SUPPORT THE CHANNEL
👍 Smash the like button
🤝 Subscribe to the channel & turn the notifications on
💬 Post comments, any feedback is greatly appreciated
THANK YOU!

LESSON 1.28

RESOURCES
Рекомендации по теме
Комментарии
Автор

Honestly, I don't know how you became this good. But every lesson blows my mind. I'm really grateful. I'm yet to finish the course, it's been beyond excellent. Thank you. God bless you.

oghenevwefepeace
Автор

This PHP course is the best beyond a shadow of a doubt!

zhozfem
Автор

this course is criminally good, thank you so much my friend

karimlaouchedi
Автор

Fantastic stuff. Trying to like every vid for you :)

edgarsvilums
Автор

I recomend widely the course, great content

arielmejia
Автор

I wish all courses were like yours, i love the way you impart them 10/10. Just I ask you that activate subtitles to help me understand you better because i from spain and i dont understand very well english talked, thank you very much🙏

madibenallou
Автор

Great work 👍
Really need such a video 😊

chabdulrahmaan
Автор

🙏🙏Wonderful loved👍👍 your presentation 😍😍and content👌👌

computerlearningbyargusaca
Автор

Really great toturial, thx for doing it .. ♥️

marwanwael
Автор

Nice course, if someone ask me that he wants to learn PHP, I'll defenitly recommend you.

mishas
Автор

Great course.
You are excellent!
Will there be a project?
Thank you very much !!

marcio
Автор

Wooow it's nice the best video of all the day (from 10 to 100 viewed today on this topic)

watchingwolf
Автор

Great content and thank you for sharing your knowledge! Are there any updates for the discord channel? I would gladly join, not a lot of PHP communities in our days.

arescatalin
Автор

These videos are great, but take a breath now and then!

bobobobee
Автор

Thanks. Could you also make a video on exception handling?

leonvanrijswijk
Автор

thanks to your excellent course . I have questions about our user defined function . is it a convention to have 4 arguments in functions ?
plz share a source if u had

atabarzegar
Автор

Video transcript for this lesson if someone needs it:
There are different types of errors that can be generated by PHP. We have fatal errors, syntax errors, notices, parse errors, and so on. Some of these errors will stop the script execution, like fatal errors, for example, but some will not like warnings. PHP determines what errors to report based on the error reporting configuration in PHP INI, which we discussed in the last lesson. And you could override that by calling error reporting explicitly within your code at runtime, which allows you to change the reporting level. If you set error reporting to zero, it will turn off the reporting, so no errors will be reported. And if you set it to EO, as mentioned before, it will report all errors, including warnings. You should always use the constants to set the reporting levels, or use the bitmask, which basically combines multiple reporting levels with bitwise operators to form the desired reporting level. For example, if you want to report everything except warnings, you could do EO and not E warning, and now this will report everything except warnings. I recommend using both EO for production and development, as I mentioned in the last lesson, especially for development, because that way you're able to catch bugs early on. Also, as mentioned before, you're able to specify the levels of reporting using the predefined constants. Let's open the list of available error -related constants in PHP docs and review them quick. For example, EError is the fatal error, and it will stop the script execution. We also see the E warning, E notice, E parse, and so on. Let's scroll down a bit, and we see constants like E underscore user, error, E underscore user warning, and so on. And these are like the other E error and E warning constants. The difference is that the ones with the user are generated manually by using the trigger error function, which we'll cover in a minute. We also have the each strict, the deprecated EO, and so on. I will leave the link to this page in the description so you could review them if you want to. So I mentioned that you're able to manually trigger errors. You can do that by calling trigger error function and passing the error message and the error level as arguments. We could also do echo one after, just to show you that this will stop the script execution because we're triggering a fatal error. If we refresh the page, we see that it triggered the fatal error and stopped the script execution because we don't see one being printed. However, if we change to E user warning and refresh, now we see triggered warning, but it did not stop the execution and one is printed on the screen. Quick note here that you can only use E underscore user constants when triggering errors manually. You cannot trigger regular E underscore warning, for example. If you do this, you will get fatal error letting you know that you must use one of the user error constants when triggering errors manually. Alright, let's continue. So when error happens in PHP, PHP will determine if the error has to be displayed or not by using the display errors configuration directive. And we talked about that in the last lesson. You should always have this turned off in production to avoid exposing some sensitive information and internal errors to the user. These errors will be logged for you to review even when you are not displaying them, as long as you have the error login turned on, which we also talked about in the last lesson. The location of error log depends on few factors, but Xampp makes it very easy to access, so you could easily open it by clicking logs and then PHP error log. We'll also talk about how to find error logs on the server itself once we get to that part of the course, which is going to be in the third section of the course. You could also manually log errors by using error log function. You could pass the message as the argument and that will be logged in that file. So how do we handle errors? We haven't talked about how to actually handle these errors. Luckily, PHP allows us to create a customer handler to tell PHP how to handle errors at runtime. This gives you the power to customize it to your needs. Maybe you want to perform some cleanup actions when the error is caught or log it in a certain way and so on. So to register a customer handler, we first need to create the function. Let's call it error handler and this function has to accept at least two arguments. The first argument has to be the error type, which is an integer, and second argument is the error message. It could also then optionally accept the file and the line number where the error happened. Now within this function, we can do whatever we want. You can put if else conditionals or switch statements to handle different types of errors differently and so on. To demonstrate how this works, I will simply print the type message file and line on screen, but you would not want to do that in production or real application because it kind of defeats the purpose of using customer handlers. Because as mentioned before, you don't want to expose your errors to the user, but if you're just going to print it in an error handler, then it doesn't really solve any issues. But I'm just going to do that for the sake of demonstration to show you how it works. You also wouldn't want to display messages unescaped like that. And don't worry about escaping or security for now. We'll talk about that later in the course. You can return false to fall back to standard PHP server handling or return something other than false like true to just continue with the script execution. You might want to do that in certain cases, maybe for certain error types, but you might also want to stop the script execution for fatal or important errors. And you can do that by using the exit statement. So once we have the function ready, now we can register it as the error handler by using the set error handler function, which accepts the customer handler callback function as the first argument and the error level as the second argument. So we could set it to EO to enable customer handling for all errors. And you can customize this to whatever error level you need. Quick note here that this customer handler will override whatever you have set for error reporting configuration setting. So if you had your error reporting set to report everything except warnings, our customer handler would override that because we are now handling all kinds of errors, including the warnings. To demonstrate that, let's comment out the customer handler and let's trigger the warning. Because this variable X has not been defined, it would trigger undefined variable warning, but because of our error reporting, we're not reporting that. If we update the error reporting, now we see the warning. Let's add back the skipping E warning part and uncomment the customer handler and move the X down and refresh the page. Now we see that even though our reporting is set to ignore warnings, the customer handler overrides that because we're specifying to handle all errors, which includes the warnings. Also, as you noticed, our customer handler worked in a printed type message file in line. Be aware that some error types can't be handled like parse and compile errors, for example. So if you have a syntax error here and we refresh page, we will get parse error. And you can see that it's not handled by the customer handler. Also, if the error happens before the script had a chance to execute to the point where it registers the error handler, it will not get handled by the customer handler. And finally, you could always restore the previous error handler by calling the restore error handler function. This is it for this video. We have covered the very basics of error handling and this is just the way to handle errors procedural way. We have not talked about something called exceptions, which are thrown by most errors in PHP and you could throw your own custom exceptions as well. Exceptions are an object oriented way of handling errors and we'll cover exceptions and error handling in more detail in the second section of the course. So thank you so much for watching. If you enjoyed this tutorial, please give this video a thumbs up and let me know in the comments if you have any questions. I'll see you on the next one.

benderbg
Автор

I actually need help, how can I configure my IDE Vs code so that warning for unused variables in php actually show because by default they don't show

nelsonke
Автор

So, are these errors and handlers like light exceptions?

alexandermatveev
Автор

what is the different between error logs, display error and error reporting ?

sym