What are the common pitfalls of including JavaScript code using PHP in a website?

One common pitfall of including JavaScript code using PHP in a website is not properly escaping the JavaScript code, which can lead to syntax errors or security vulnerabilities. To avoid this issue, you should use functions like htmlspecialchars() to escape the JavaScript code before outputting it to the browser.

<?php
$javascriptCode = "<script>alert('Hello, World!');</script>";
echo "<script>" . htmlspecialchars($javascriptCode) . "</script>";
?>