How can PHP variables be properly passed as strings to JavaScript functions in HTML?

When passing PHP variables as strings to JavaScript functions in HTML, you can use the `json_encode()` function to properly encode the variable as a JSON string. This ensures that the variable is correctly formatted and can be passed as a string to JavaScript functions without any issues.

<?php
$php_variable = "Hello, World!";
?>
<script>
var js_variable = <?php echo json_encode($php_variable); ?>;
console.log(js_variable);
</script>