How to integrate javascript into php?

Syntax errors appear when adding javascript to a php script.
Tell me, is this method real at all?

<?  echo "<script type='text/javascript'>
  function vibor() 
  { 
    var kp = 'znachenie';
    document.write(kp); 
  }; 
</script>";
?>

Or what to pay attention to, maybe some of the quotes should be escaped?


Answer 1, authority 100%

Yes, write something like:

echo <<<htmlcode
<script type='text/javascript'> 
    function vibor() 
    { 
      var kp = 'znachenie'; 
      document.write(kp); 
    }; 
  </script>
htmlcode;

Echo example


Answer 2, authority 100%

That’s the whole point. It is necessary to assign a value from JS to a PHP variable. Or immediately output the value via echo or printf.

http://phpfaq.ru/na_tanke


Answer 3, authority 67%

Hmm, maybe just replace:

<?  echo "<script type='text/javascript'>
  function vibor() 
  { 
    var kp = 'znachenie';
    document.write(kp); 
  }; 
</script>";
?>

On

<script type='text/javascript'>
  function vibor() 
  { 
    var kp = 'znachenie';
    document.write(kp); 
  }; 
</script>";

i.e. do not pointlessly open a tag in order to display static content