How can the PHP version compatibility be ensured when developing a website locally and deploying it online?

When developing a website locally and deploying it online, PHP version compatibility can be ensured by checking the PHP version on both the local development environment and the online server. This can be done by using the phpversion() function to retrieve the PHP version number and then making sure that the code is compatible with the lowest version supported by both environments.

// Check PHP version compatibility
$php_version = phpversion();

if (version_compare($php_version, '7.0.0', '<')) {
    // Code that is only compatible with PHP 7.0.0 or higher
} else {
    // Code that is compatible with PHP 7.0.0 or higher
}