What are common pitfalls when trying to pass PHP variables to HTML input fields?

One common pitfall when passing PHP variables to HTML input fields is not properly escaping the variables, which can lead to security vulnerabilities such as cross-site scripting (XSS) attacks. To solve this issue, always use htmlspecialchars() function to escape the variables before outputting them in the HTML input fields.

<?php
// Example PHP variable
$name = "John Doe";

// Escaping the variable before outputting it in the HTML input field
echo '<input type="text" name="name" value="' . htmlspecialchars($name) . '">';
?>