I am trying to print the string of a PHP variable in a Javascript function.Whenever i use a new line in the string that is stored in the PHP variable, I get this error Uncaught SyntaxError while printing it out in a Javascript function.
The JavaScript exceptions "unexpected token" occur when a specific language construct was expected, but something else was provided. This might be a simple typo.
To solve the "Uncaught SyntaxError: Unexpected identifier" error, make sure you don't have any misspelled keywords, e.g. Let or Function instead of let and function , and correct any typos related to a missing or an extra comma, colon, parenthesis, quote or bracket.
Last reviewed in August 2021. The error Uncaught SyntaxError: Unexpected token < is most commonly caused by your site's code referring to an asset that is no longer available. Most commonly, this is due to a filename change in assets generated during your build.
If you want to declare a string variable across multiple lines, you could use this syntax:
var str = `line 1
line 2
line 3`;
Note the (`) instead of (")/(') double or single quotes. They can't be used to declare multiline string.
Essentially, you will need to write it like this:
jQuery("body").replaceWith(`<?php echo $PHPVARIABLE?>`)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With