What is the best practice for passing PHP variables to a form field in a web application?

When passing PHP variables to a form field in a web application, it is best practice to use the `htmlspecialchars` function to prevent cross-site scripting (XSS) attacks. This function converts special characters to HTML entities, ensuring that user input is properly sanitized before being displayed in the form field.

<?php
// Define a PHP variable
$variable = "Hello, World!";

// Pass the PHP variable to a form field using htmlspecialchars
echo '<input type="text" name="field" value="' . htmlspecialchars($variable) . '">';
?>