How can PHP developers balance the preservation of legacy code for historical purposes with the need to update and modernize code to ensure compatibility with current PHP versions and best practices?

To balance the preservation of legacy code with the need to update and modernize, PHP developers can create a separate branch in their version control system for legacy code, allowing them to maintain historical versions while working on updates in the main branch. They can gradually refactor the legacy code to adhere to current PHP versions and best practices, ensuring compatibility without compromising historical integrity.

// Example of gradually refactoring legacy code to adhere to current PHP versions and best practices
// Legacy code
function calculateSum($a, $b) {
    return $a + $b;
}

// Updated code with type declarations and explicit return type
function calculateSum(int $a, int $b): int {
    return $a + $b;
}