What potential pitfalls should be considered when writing user input directly into a PHP file?

When writing user input directly into a PHP file, one potential pitfall to consider is the risk of code injection attacks. To mitigate this risk, it is important to sanitize and validate user input before using it in your PHP code. This can be done by using functions like htmlspecialchars() to escape special characters and prevent malicious code from being executed.

$user_input = $_POST['user_input'];
$clean_input = htmlspecialchars($user_input);
// Use $clean_input in your PHP code