What is the best practice for dynamically generating form fields in PHP?

When dynamically generating form fields in PHP, it is best practice to use a loop to iterate through an array of field names and generate the corresponding form fields. This allows for flexibility in adding or removing fields without having to manually code each one.

<?php
// Array of field names
$fields = ['name', 'email', 'phone'];

// Loop through the array and generate form fields
foreach ($fields as $field) {
    echo '<label for="' . $field . '">' . ucfirst($field) . ':</label>';
    echo '<input type="text" name="' . $field . '" id="' . $field . '"><br>';
}
?>