How can PHP strings be made compatible with JavaScript without causing errors?

When passing PHP strings to JavaScript, special characters like single quotes, double quotes, and line breaks can cause syntax errors. To avoid this, you can use the `json_encode()` function in PHP to properly escape these characters before outputting the string in JavaScript.

<?php
$phpString = "Hello, 'World'!";
$jsString = json_encode($phpString);
?>

<script>
var jsString = <?php echo $jsString; ?>;
console.log(jsString);
</script>