How can a PHP developer ensure that their code is compatible with different PHP versions when hosting on a server they do not own?

When hosting PHP code on a server they do not own, a PHP developer can ensure compatibility with different PHP versions by using version-specific features sparingly, checking for deprecated functions, and testing their code on different PHP versions using tools like PHP Compatibility Checker or running it in a local development environment with the target PHP version.

// Example code snippet demonstrating how to check for deprecated functions
if (function_exists('mysql_connect')) {
    // Use mysqli or PDO instead of mysql functions
    $db = mysqli_connect('localhost', 'username', 'password', 'database');
} else {
    // Fallback code for older PHP versions
    $db = mysql_connect('localhost', 'username', 'password');
}