How can error handling be improved in the PHP script to prevent similar issues in the future?

Issue: In the current PHP script, error handling is not properly implemented, leading to potential issues such as uncaught exceptions and undefined variables. To prevent similar issues in the future, we can improve error handling by using try-catch blocks to catch exceptions and using isset() function to check for the existence of variables before using them.

try {
    // code that may throw an exception
    $result = 1 / 0;
} catch (Exception $e) {
    // handle the exception
    echo "An error occurred: " . $e->getMessage();
}

// check if a variable is set before using it
if (isset($variable)) {
    // code that uses the variable
    echo $variable;
} else {
    echo "Variable is not set";
}