How can outdated PHP functions like mysql_ and mail() be replaced with more modern and secure alternatives?

Outdated PHP functions like mysql_ and mail() should be replaced with more modern and secure alternatives like PDO for database operations and PHPMailer for sending emails. This helps to prevent security vulnerabilities and ensure compatibility with newer PHP versions.

// Replace mysql_ functions with PDO for database operations
$pdo = new PDO('mysql:host=localhost;dbname=database', 'username', 'password');
$stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id');
$stmt->execute(['id' => $userId]);
$user = $stmt->fetch();

// Replace mail() function with PHPMailer for sending emails
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'vendor/autoload.php';

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.example.com';
$mail->SMTPAuth = true;
$mail->Username = 'user@example.com';
$mail->Password = 'password';
$mail->setFrom('from@example.com', 'Your Name');
$mail->addAddress('recipient@example.com', 'Recipient Name');
$mail->Subject = 'Subject';
$mail->Body = 'Email body';

$mail->send();