How to output JavaScript with PHP

前端 未结 12 1535
无人及你
无人及你 2020-11-27 06:16

I am new to PHP. I need to output the following JavaScript with PHP. This is my code:





        
12条回答
  •  一生所求
    2020-11-27 06:40

    The error you get if because you need to escape the quotes (like other answers said).

    To avoid that, you can use an alternative syntax for you strings declarations, called "Heredoc"

    With this syntax, you can declare a long string, even containing single-quotes and/or double-quotes, whithout having to escape thoses ; it will make your Javascript code easier to write, modify, and understand -- which is always a good thing.

    As an example, your code could become :

    $str = <<
      document.write("Hello World!");
    
    MY_MARKER;
    
    echo $str;
    

    Note that with Heredoc syntax (as with string delimited by double-quotes), variables are interpolated.

提交回复
热议问题