How can error messages be stored in an array and displayed on the form page without using Location headers?

When error messages need to be stored in an array and displayed on the form page without using Location headers, you can achieve this by storing the error messages in an array variable and then displaying them on the form page using PHP. You can check for form validation errors, store them in an array, and then loop through the array to display each error message on the form page.

<?php
$errors = [];

// Check for form validation errors
if(empty($_POST['username'])){
    $errors[] = "Username is required";
}

if(empty($_POST['password'])){
    $errors[] = "Password is required";
}

// Display error messages on the form page
if(!empty($errors)){
    echo "<ul>";
    foreach($errors as $error){
        echo "<li>$error</li>";
    }
    echo "</ul>";
}
?>