How can the EVA principle be applied to PHP scripts to ensure a smoother user experience and prevent data loss during form submissions?

Issue: The EVA principle (Error, Validation, Action) can be applied to PHP scripts by first checking for errors, validating user input, and then executing the desired action to ensure a smoother user experience and prevent data loss during form submissions.

// Check for form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Error handling
    $errors = [];

    // Validation
    if (empty($_POST["username"])) {
        $errors[] = "Username is required.";
    }

    if (empty($_POST["email"])) {
        $errors[] = "Email is required.";
    } elseif (!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
        $errors[] = "Invalid email format.";
    }

    // If no errors, proceed with action
    if (empty($errors)) {
        // Process form submission
        // Insert data into database, send email, etc.
    } else {
        // Display errors to user
        foreach ($errors as $error) {
            echo "<p>$error</p>";
        }
    }
}