You really don’t need all of those useless variables. You’re basically reassigning variables to new variables and only using those new variables. Why not cut to the chase and just use your desired variable names?
You would think so, but no. Php is well suited to handle its errors on it’s own. Now, what you could do is use set_exemption_handler to customize how exceptions are handled. You now have one place to handle exceptions without littering your code base. There are only a couple instances where you would implement a try/catch and that is with the DB connection and DB duplicate errors. And you for sure don’t want to output internal system errors to the user. That information is only good to hackers and useless to the user.
Hi @benanamen please correct me if I’m wrong. My understanding is that we can use set_exempion_handler to handle all uncaught exceptions, but use try catch block if we want to create a custom exception message, is it correct?
I think @benanamen is alluding to the fact that you shouldn’t be littering your code with try/catch blocks. My understanding of why you shouldn’t try to “catch 'em all” is because you should already know what kind of errors your code produces. The only time when you should technically use try/catch blocks is on things that happen internally where you may not know the root cause of an issue. benanamen gave a great example of that. Usually when the database breaks for some reason, you don’t want to be carrying that broken connection over so you might want to catch those errors. Things like this do actually happen randomly. An example can be the database engine just shutting off randomly due to maybe a corrupted data or file. Another example is maybe the database engine breaking from an update. All of these things, you can’t really control. That’s when it would be plausible to use a try/catch block. If it’s just something logical, you should already be expecting some false positive or positive results. This is all logical.
There are many, many “tutorials” out there. I think it is safe to say that most of them get it “wrong”, or at least parts of it wrong including the one you just posted. Consider them guides until you know better.