I am looking for a way to tell the server to die when there is an error.
Examples: Let's say there are 2SQL queries. If one of them does an error, I want none of them to work, as if nothing has been clicked.
Example 2: When you miss a semicolon or similar on your code, but the other parts of your code still works and displays something that isn't supposed.
Something like
if (error) { do nothing and go back to index page; }
Exception handling is available in PHP since version 5. It allows you to have a more fine-grained control over code when things go wrong ie, when exceptions occur.
Put your codes inside try block. if an error occur then the code inside your catch block will run. There is also one more bock. i.e finally (PHP 5.5) which will be called every time your code will run. You can hierarchically use these blocks.
Useful link: https://adayinthelifeof.nl/2013/02/12/php5-5-trycatchfinally/
try {
//Your codes
} catch (Exception $e) {
//when above codes gives error
do nothing and go back to index page;
}
You can always send a redirect and call die()/exit()
to stop execution of the current script.
if (error) {
header("Location: http://example.com/index.php");
die();
}
Or surround your code with try/catch and send the redirect inside the catch block:
try {
//code where an error could occur
} catch (Exception $e) {
header("Location: http://example.com/index.php");
die();
}