How can PHP be used to automatically redirect to a different file after a button click?

To automatically redirect to a different file after a button click in PHP, you can use the header() function to send a raw HTTP header to the browser with the new location. You can do this by checking if a specific button has been clicked using a form submission or AJAX request, and then using the header() function to redirect the user to the desired file.

<?php
if(isset($_POST['button_click'])) {
    header("Location: newfile.php");
    exit();
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Redirect after Button Click</title>
</head>
<body>
    <form method="post">
        <button type="submit" name="button_click">Click me to redirect</button>
    </form>
</body>
</html>