What are the best practices for writing PHP code to avoid compatibility issues?

To avoid compatibility issues when writing PHP code, it is important to follow best practices such as using the latest PHP version, avoiding deprecated functions, and using proper error handling techniques. Additionally, it is recommended to test your code on different PHP versions to ensure compatibility.

<?php
// Example of using the latest PHP version
declare(strict_types=1);

// Avoid deprecated functions
if (function_exists('deprecated_function')) {
    // Use alternative function
} else {
    // Use the current function
}

// Proper error handling
try {
    // Your code here
} catch (Exception $e) {
    // Handle errors
}
?>