What are the best practices for updating PHP scripts to ensure compatibility with PHP 7, specifically in terms of replacing deprecated functions like mysql_connect?

To ensure compatibility with PHP 7 and replace deprecated functions like mysql_connect, it is recommended to switch to using the mysqli or PDO extension for database connections. This involves updating the code to use the mysqli_connect() function instead of mysql_connect(). Additionally, it is important to handle errors and exceptions properly when making these changes to ensure the script functions correctly.

// Replace deprecated mysql_connect with mysqli_connect
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

$conn = mysqli_connect($servername, $username, $password, $dbname);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}