0

I'm new to PHP and MySQL, so I read a lot of tutorials and things like that. In most of them, the author stops the script by using the die(); or exit (); function. I've also read that this is a bad way to end a script.

Now I want to code something by myself and I wonder if there is a better way to end a script? Is it better to use trigger_error? And what's the difference to die();? Or should I use a lot of else's and if's to avoid using die() at all?

I've searched a lot on the internet about this but couldn't find a clear answer. Seems like everyone is using just die().

Furthemore, I Know that you can use try/catch with PDO, but at the moment I'm just using mysqli and want to know if there is a better way without using PDO.

I'm very grateful for every meaningful answer!

Sphex
  • 45
  • 1
  • 5
  • 2
    throw new Exception(...) ? – Gogol May 16 '16 at 12:00
  • 2
    This is a matter of preference; writing to a `.log`, use of `Exceptions` to name a few. Secondly, it depends on the stage of development - `die()` isn't graceful for production. – MackieeE May 16 '16 at 12:00
  • `die;`/`exit;` isn't a bad way to end a script, it's *how* you prematurely end a script. Granted it's horrible for a production environment (due to UX), but it's not a bad way to end a script. – ʰᵈˑ May 16 '16 at 12:02
  • die() is not a bad way to end a script, but it's a bad way to handle errors. So, if you just want to end the script die() is fine, but if you want to end it because it triggered an error detection you should always use an error handler. – holyknight May 16 '16 at 12:02
  • Using `exit()` or `die()` to terminate a script at a specific point is similar to using `return;` or `return X;` to terminate a function at a specific point. In that sense it's perfectly valid coding practice. – apokryfos May 16 '16 at 12:03
  • To answer your question one needs to know why do you want to die. – Your Common Sense May 16 '16 at 12:04
  • Using `die()` or `exit()` is handy for debugging (at least for me). For example you can write `print_r(variable); exit();` to print the contents of `variable` at any part of the script. In production environment it is not the way to go, so use Exceptions and/or logging. – akasummer May 16 '16 at 12:04
  • 1
    I'm assuming, by the reference to the database driver that you're almost explicitly referring to when connecting to the database `... or die()` sort of thing? Rather than say, exiting the script after a `header( ... )` redirect. – CD001 May 16 '16 at 12:05
  • Second this. On the second thought I have a feeling that you are referring to die() as to some sort of error handler. And thus your question "How to handle errors properly." – Your Common Sense May 16 '16 at 12:08
  • This is more an opinion based question. `exit`/`die` is a nice language construct. Since PHP can also be used as command line script, there's a need for it. Linux shells read the exit code and consider any code<>0 as an error. There are some believes that there should not be more than one exit point of a function or a script. Of course it could be difficult if you have no idea, where you code is actually terminating. You can use `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` if you want mysqli exeptions. – Pinke Helga May 16 '16 at 12:12
  • @Your Common Sense I think I don't want to use die as an error handler. For example in a registration code: I want to end the code, when the user wants to insert a username which is already given in the DB.Is this the same as error handling? And is it okay to use die or exit in such a case? Sorry, I should have mentioned this earlier. – Sphex May 16 '16 at 12:27
  • There are system errors and business logic errors. And a duplicate username is of the latter kind. But business logic error are not really an error. For the application logic they are just responses sent to the client usual way. Instead, just a response have to be sent to the client usual way. There is no essential difference between sending "Your registration is done" or "You cannot use this username". There fore there is not a single reason to abort the script execution in case of a business logic error. – Your Common Sense May 16 '16 at 12:34
  • btw.: I don't like relying on conditional `or` operator in most languages, except perl. Perl is designed to use short notations and I do not expect they will implement some optimizer some day. However, I am not sure if future PHP version won't evaluate e.g. `fn1(1) + fn2(2) == 1 && fn1(2)>fn2(1) || $x = 1` in a performance optimized order. `$x = 1` is trivial and could be evaluated first. Relying on condition abuse then would mean having to adapt tons of old code on migration. – Pinke Helga May 16 '16 at 12:41
  • @Your Common Sense Okay, it's a business logic error and therefore not realy an error. But - I know it may be outdated and maybe bad - your PHP code is procedural. So if you do not exit the code after asserting that the username is duplicate, it is inserted in the DB. Of course, i can put some if's and else's around the insert to prohibit this, but I've wondered if it would be easier to just exit the code. But I've heard that's a bad way. – Sphex May 16 '16 at 16:31
  • Yes, and I already told you - why. It's a usability problem. You have to use ifs not to avoi dyeing. You have to use them to improve the user experience. in essence, you are asking, "Am I allowed to write a dirty outdated code with frustrating user experience?". Well technically nobody forbids you. – Your Common Sense May 16 '16 at 16:47
  • @Your Common Sense Yes, it really sounds like I'm searching for a lazy way to handle things like that. I think I've expressed myself in a wrong way. However, I was just wondering about the right way to handle business logic errors, because I've seen a lot of people using die or exit. So I thought it would be right or maybe faster. But I've recently tested some things out and saw the bad UX. And unfortunately I've just learned the procedural way of coding with PHP. So I think I have to study harder and maybe switch to PDO. But all in all thanks a lot for your answers! – Sphex May 16 '16 at 17:20
  • For one of your confusions I've got the answer. 95% of PHP code is just a trash. Awful trash. So, no wonder you are you have seen a lot of die and exit. In fact, it's a biggest problem of PHP - people learn wrong ways, then post bad code, then other people learn from it - and so on, an eternal circle. So I am an a sort of crusade against it, tryig to show people that proper ways can be simpler than dirty ones. If you will have any further questions, feel free to ask. You may leave a comment on my site any time, I'll be happy to answer. – Your Common Sense May 16 '16 at 17:37

0 Answers0