How can error handling be implemented in the "sendSMS" function to address possible issues with the SMS sending process?
Issue: Error handling can be implemented in the "sendSMS" function by using try-catch blocks to catch any exceptions that may occur during the SMS sending process. This allows for graceful handling of errors and prevents the script from crashing if an issue arises.
function sendSMS($phoneNumber, $message) {
try {
// Code to send SMS
$response = send_sms_to_phone_number($phoneNumber, $message);
if($response['status'] == 'success') {
echo "SMS sent successfully!";
} else {
throw new Exception("Failed to send SMS");
}
} catch (Exception $e) {
echo "Error: " . $e->getMessage();
}
}
Related Questions
- What are some best practices for handling character encoding in PHP, specifically when working with UTF-8?
- How can multiple PHP echo statements be handled and processed individually in JavaScript for further computation?
- How can the combination of htmlspecialchars and mysql_real_escape_string affect the output and storage of data in PHP applications?