Are there any specific best practices for passing PHP variables in JavaScript functions within HTML elements?

When passing PHP variables in JavaScript functions within HTML elements, it is best practice to echo the PHP variable value within a data attribute in the HTML element. This allows you to access the PHP variable value in JavaScript using the dataset property. By doing this, you can ensure that the PHP variable value is properly passed and utilized in your JavaScript functions.

<?php
$php_variable = "Hello, World!";
?>

<button id="myButton" data-phpvar="<?php echo $php_variable; ?>">Click me</button>

<script>
document.getElementById('myButton').addEventListener('click', function() {
    var phpVar = this.dataset.phpvar;
    alert(phpVar);
});
</script>