What are the potential pitfalls of using multiple if-cases for form validation in PHP?

Using multiple if-cases for form validation in PHP can lead to code duplication, making it harder to maintain and update the validation logic. To solve this issue, you can use a more structured approach like creating a separate function for form validation and centralizing all validation rules in one place.

<?php

function validateForm($formData) {
    $errors = [];

    if (empty($formData['name'])) {
        $errors['name'] = 'Name is required';
    }

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

    // Add more validation rules as needed

    return $errors;
}

// Usage
$formData = $_POST;

$errors = validateForm($formData);

if (!empty($errors)) {
    // Handle errors, display to user, etc.
} else {
    // Form is valid, proceed with processing
}