What are common errors to look out for when embedding JavaScript within PHP code?

One common error when embedding JavaScript within PHP code is not properly escaping characters, which can lead to syntax errors or unexpected behavior. To avoid this issue, use the PHP `htmlspecialchars` function to escape any special characters in the JavaScript code before embedding it.

<?php
$javascript_code = '<script>alert("Hello, world!");</script>';
$escaped_javascript_code = htmlspecialchars($javascript_code);
echo "<script>$escaped_javascript_code</script>";
?>