What are the differences between MySQLi and MySQL in PHP, and how can developers ensure compatibility when switching between the two?

When switching between MySQLi and MySQL in PHP, developers need to be aware of the differences in syntax and functions between the two. To ensure compatibility, developers can use conditional statements to check which extension is available and use the appropriate functions accordingly.

// Check if MySQLi extension is available
if (function_exists('mysqli_connect')) {
    // Use MySQLi functions
    $conn = mysqli_connect($servername, $username, $password, $dbname);
} else {
    // Use MySQL functions
    $conn = mysql_connect($servername, $username, $password);
}