PHP Basics

Error Handling

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
PHP Basics

Check out a free preview of the full PHP Basics course

The "Error Handling" Lesson is part of the full, PHP Basics course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano shares some tips for handling errors in PHP applications. Try/Catch blocks can hand exceptions. The PHP server settings can be modified through the "ini_" functions to change the error reporting levels.

Preview
Close
Get $100 Off
Get $100 Off!

Transcript from the "Error Handling" Lesson

[00:00:01]
>> Maximiliano Firtman: Again, it's not the most performant one, there are a lot of work to do, now we are on the details, we are going to the database again and bringing them all, and maybe we should bring only the one I want. I should check for errors, what happens if I'm passing an index, well, that doesn't exist, for example, it's in the URL now, so I can change the number.

[00:00:26]
Yeah, okay, well it's not working, so what we should be doing here, how to deal with these errors? And that leads to the final topic that I have to talk to you, it's about error management. Okay, so how to deal with error in PHP? Errors in PHP are complex because there are so many ways to treat errors, and it depends on what are you using, it's one way or the other, okay?

[00:00:58]
So let me start saying that on some situations, you can use try catch, so for example, I can try, I mean the details, yes, if you're coming from other languages, you probably know try catch. So I can try to execute these two lines, and I will catch in this, maybe, but it doesn't work like that, try catch needs something else.

[00:01:25]
It needs a type, say, what type, why is that? Because it's following Java pattern, not sure how many of you have played with Java or C sharp as well, but not JavaScript, it's like you can have more than one catch. So it's possible to have more than one catch, and actually, you will catch different exceptions, so, you have to put there which exception do you wanna catch.

[00:01:50]
Exception is actually the base class, so that means you are trapping all the possible exceptions, okay? And by the way, here comes the part where it start doing weird things, so let's say you wanna echo here, error, okay? So you say, okay, error, so it's not working, or you can just put HTML here.

[00:02:15]
Okay, so I refresh, and am I seeing the error? No, why is that? There may be many cases of what's going on here, one is that this is not an exception, it's another kind of error. As I mentioned not everything can be trapped with an exception, which is a problem, because there are many ways to trap errors.

[00:02:37]
So errors in JavaScript can be tricky, to be honest, so it's not really simple, and sometimes, I think that it happened to some of you in the chat, sometimes you don't see the errors, because if you look, these errors are warnings. Okay, because they are warnings, you may have warnings disabled in your current project.

[00:03:02]
To solve the problem, there are two ways you can go to your document, your configuration, PHP file and change that, but sometimes you don't have permission to that. So the other thing that you can do is you can request all errors to be reported, and you do that with these lines, I have been using this for a while.

[00:03:27]
You say, ini_set. What is ini? It's a file, PHP.ini, i and i is the configuration file, so you can set something from here and you say, ("display_errors", 1), which means, yeah, I wanna see the errors. You also change errors, you also change display startup errors just in case, it's another array of errors and you say, yeah, also.

[00:03:55]
Also you're going to execute error reporting and you will change that to E_All. E_All, what is that? Well, it's a list of constants starting with E underscore, and you can see all the others we have. So I wanna see all the core errors, only warnings, and actually you can say, I wanna see warnings, and also I wanna see user errors, or I wanna see all.

[00:04:28]
Okay, and you need to put it on every PHP file, but this is for developer purposes, don't do that in production, because that means in production, your user will see details of your URLs, your PHP file, sometimes you will be exposing data, private data that should be exposed.

[00:04:47]
Okay, so that's not how you should be doing, right now it's not going to make any difference because mine were already turned on, okay? You have a question face now you have [INAUDIBLE]
>> Speaker 2: So It says line 22.
>> Maximiliano Firtman: Well, that line 22 this one, but first one, we have another one here, actually, we have three warnings.

[00:05:08]
>> Speaker 2: I see.
>> Maximiliano Firtman: That they're being executed in order and because the errors are within my HTML, they're taking the CSS also, that's why one is bigger, that's because it's more important.
>> Speaker 2: Understand.
>> Maximiliano Firtman: My first one is on line 15.
>> Speaker 2: Got it.
>> Maximiliano Firtman: So I need to go and say okay, line 15 is this one.

[00:05:25]
>> Speaker 2: Got it.
>> Maximiliano Firtman: So actually, it says undefined array key 1230, because, yeah, you know what, it's not null, that's why it's not taking the 0, it's a different problem. It wasn't I do have something, but actually, when I've tried to get into that, so how to solve the problem?

[00:05:47]
Well, actually, you to first check, maybe, you need to go and say, well, the index that I want is this one, I should check if this index is not or is.
>> Maximiliano Firtman: Lower than the count of exhibits that I have. Okay, well, then you need to do this manually, and then this is index and do something, and what they're going to do here also doing an echo with error.

[00:06:20]
But if you do that, this is details, right? Yeah, nothing happens because it's still executing the rest of the code, and I'm still executing the rest of the code. So sometimes, when you encounter an error, something that you can do, is you can kind of stop the execution.

[00:06:41]
You can say, hey, you know what, I'm going to exit, I'm going to terminate this here, and sometimes when you do that, you also wanna forward the user to another page that says error 500. I'm sorry, this is not working, I don't know what happened, I can't manage the problem, so let's go to another page.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now