What is the role of templates in handling URL redirection in PHP applications?

When handling URL redirection in PHP applications, templates can be used to easily manage and organize the redirection logic. By creating a template for redirection, you can centralize the code for handling different types of redirects, making it easier to maintain and update in the future.

<?php
// Redirect function using a template
function redirect($url, $statusCode = 302) {
    header('Location: ' . $url, true, $statusCode);
    exit();
}

// Example usage
redirect('https://www.example.com', 301);
?>