What are potential pitfalls to watch out for when using if statements to handle form inputs in PHP?

One potential pitfall when using if statements to handle form inputs in PHP is not properly validating and sanitizing user input, leaving the application vulnerable to security risks such as SQL injection or cross-site scripting attacks. To mitigate this risk, always validate and sanitize user input before processing it in your application.

// Example of validating and sanitizing user input in PHP
if(isset($_POST['submit'])){
    $username = isset($_POST['username']) ? htmlspecialchars($_POST['username']) : '';
    $password = isset($_POST['password']) ? htmlspecialchars($_POST['password']) : '';

    // Validate input
    if(empty($username) || empty($password)){
        echo "Please fill in all fields.";
    } else {
        // Sanitize input
        $username = filter_var($username, FILTER_SANITIZE_STRING);
        $password = filter_var($password, FILTER_SANITIZE_STRING);

        // Process input
        // Your code here to handle the form inputs
    }
}