What best practices should be followed when validating user input in a PHP sign-up form?

When validating user input in a PHP sign-up form, it is important to check for both client-side and server-side validation. Client-side validation can be done using JavaScript to provide immediate feedback to the user, while server-side validation ensures that the data submitted meets the required criteria and is safe to process. Common validation checks include checking for empty fields, validating email addresses, enforcing password requirements, and sanitizing input to prevent SQL injection attacks.

// Server-side validation example
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    if (empty($name) || empty($email) || empty($password)) {
        echo "Please fill out all fields.";
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        echo "Invalid email format.";
    } elseif (strlen($password) < 8) {
        echo "Password must be at least 8 characters long.";
    } else {
        // Process sign-up form data
    }
}