I believe that everything will depend on the context that you are working.
The side 'bad' to use the die
is that your execution will end up right there. Regardless of the error.
Using other methods such as try/catch
you will have the opportunity to work with Exception
to customize and minimize failure.
try {
$sql = $mysqli->query( "SELECT * FROM tabela" );
} catch (Exception $e) {
//$e->getMessage();
//Aqui você pode redirecionar pra outra página, exibir uma mensagem personalizada ou qualquer coisa melhor do que parar sua aplicação.
}
You can still use both together in specific cases:
try {
$sql = $mysqli->query( "SELECT * FROM tabela" );
} catch (Exception $e) {
try {
//faça algo como segunda opção
} catch (Exception $i) {
die ('Falha: ' . $i->getMessage());
}
}
I think everything will depend on the context and how much you want to keep your user in your application.
I hope I’ve helped.
Source
I practically answered here a few hours ago: http://answall.com/a/141797/70 (but as you mentioned, I complemented and highlighted the relevant part)
– Bacco
Some say that every PHP script should start with
die()
:D– rray