What benefits does wrapping the file comparison and copying logic in a function provide in PHP programming?

Wrapping the file comparison and copying logic in a function helps to encapsulate the functionality, making the code more modular and reusable. It also improves readability and maintainability by separating the specific logic into its own function. Additionally, it allows for easier testing and debugging of the file comparison and copying process.

function compareAndCopyFiles($sourceFile, $destinationFile) {
    if (file_exists($sourceFile)) {
        if (file_exists($destinationFile)) {
            unlink($destinationFile);
        }
        copy($sourceFile, $destinationFile);
        echo "File copied successfully.";
    } else {
        echo "Source file does not exist.";
    }
}

// Example usage
compareAndCopyFiles('source.txt', 'destination.txt');