How can PHP variables be efficiently integrated into JavaScript code for dynamic content generation?

To efficiently integrate PHP variables into JavaScript code for dynamic content generation, you can use PHP to echo the variable values directly into the JavaScript code. This way, the PHP variables will be dynamically inserted into the JavaScript code when the page is rendered.

<?php
$dynamic_content = "Hello, world!";
?>

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