What are some best practices for embedding PHP code in JavaScript using document.write?

Embedding PHP code in JavaScript using document.write can be tricky because PHP is a server-side language and JavaScript is a client-side language. One common approach is to use PHP to generate JavaScript code that includes the PHP variables or data you want to pass to the client-side script. This way, the PHP code is executed on the server before the JavaScript code is sent to the client, ensuring that the PHP variables are properly embedded in the JavaScript code.

<?php
$php_variable = "Hello, World!";
?>
<script>
document.write('<?php echo $php_variable; ?>');
</script>