How can JavaScript functions like onClick be properly integrated into HTML templates in PHP projects?

To properly integrate JavaScript functions like onClick into HTML templates in PHP projects, you can echo the JavaScript code within the PHP file where the HTML template is being generated. This way, the JavaScript function will be included in the final HTML output and can be triggered as expected.

<?php
// PHP code to generate HTML template
echo '<button onclick="myFunction()">Click me</button>';

// JavaScript function
echo '<script>
function myFunction() {
  alert("Button clicked!");
}
</script>';
?>