How to output JavaScript with PHP

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

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





        
12条回答
  •  萌比男神i
    2020-11-27 06:52

    You should escape the JavaScript string delimiters inside the PHP string. You're using double quotes for both PHP and JavaScript strings. Try like this instead:

    
    
    ';
    echo 'document.write("Hello World!")';
    echo '';
    
    ?>
    
    
    

    You have to escape quotes on both JavaScript and PHP when the string delimiter are the same as the quotes:

    echo "\""; // escape is done using a backslash
    echo '\'';
    

    Same in JavaScript:

    alert("\""); // escape is done using a backslash
    alert(echo '\'');
    

    But because it's very hard to read a string with such escape sequences, it is better to combine single with double quotes, as needed:

    echo '"';
    echo "'";
    

提交回复
热议问题