What best practices should be followed when structuring PHP functions to handle HTML generation and JavaScript event handling for dynamic select lists?

When structuring PHP functions to handle HTML generation and JavaScript event handling for dynamic select lists, it is important to separate the PHP logic from the HTML output and JavaScript event handling. This can be achieved by creating separate functions for generating the HTML select options, generating the JavaScript event handlers, and combining them in the main function that outputs the final HTML.

<?php
function generateSelectOptions($options) {
    $html = '';
    foreach ($options as $option) {
        $html .= '<option value="' . $option['value'] . '">' . $option['label'] . '</option>';
    }
    return $html;
}

function generateJavascriptEventHandler($selectId) {
    $js = '<script>';
    $js .= 'document.getElementById("' . $selectId . '").addEventListener("change", function() {';
    $js .= 'console.log("Selected value: " + this.value);';
    $js .= '});';
    $js .= '</script>';
    return $js;
}

function generateDynamicSelectList($selectId, $options) {
    $html = '<select id="' . $selectId . '">';
    $html .= generateSelectOptions($options);
    $html .= '</select>';
    $html .= generateJavascriptEventHandler($selectId);
    return $html;
}

$options = [
    ['value' => '1', 'label' => 'Option 1'],
    ['value' => '2', 'label' => 'Option 2'],
    ['value' => '3', 'label' => 'Option 3']
];

echo generateDynamicSelectList('dynamicSelect', $options);
?>