I am new to PHP. I need to output the following JavaScript with PHP. This is my code:
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 "'";