What are some common mistakes that PHP developers make when handling text input from forms in PHP?

One common mistake is not properly sanitizing user input, which can lead to security vulnerabilities such as SQL injection or cross-site scripting attacks. To prevent this, developers should always sanitize and validate input before using it in their code. Another mistake is not properly escaping output when displaying user input on a webpage, which can also lead to security vulnerabilities. Developers should use functions like htmlspecialchars() to escape output and prevent XSS attacks.

// Sanitize user input using filter_input() function
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);

// Validate user input using regular expressions
if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
    echo "Invalid username format";
}

// Escape output using htmlspecialchars() function before displaying on webpage
echo htmlspecialchars($username);