How can PHP variables be properly referenced in HTML forms?

PHP variables can be properly referenced in HTML forms by echoing the variable within the value attribute of the form input elements. This allows the PHP variable's value to be displayed in the form field and submitted with the form data.

<?php
// Define a PHP variable
$name = "John";

// Output an HTML form with the PHP variable referenced in the value attribute
echo '<form action="submit.php" method="post">
        <input type="text" name="name" value="' . $name . '">
        <input type="submit" value="Submit">
      </form>';
?>