How can escaping collisions be avoided when using PHP to output JavaScript functions in HTML code?

When outputting JavaScript functions using PHP in HTML code, it's important to properly escape any special characters to avoid collisions. One way to do this is by using the `json_encode()` function in PHP, which will properly encode the JavaScript code to prevent any conflicts.

<?php
$jsFunction = "function myFunction() {
    alert('Hello World!');
}";

echo "<script>";
echo json_encode($jsFunction);
echo "</script>";
?>