7

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.

enter image description here

2
  • 1
    Do not post pictures. Paste your code here instead, and show us what you have tried. Aug 13, 2017 at 14:30
  • ok sir. i am new in stackoverflow.next time i will paste my code.Thanks Aug 13, 2017 at 14:32

1 Answer 1

26

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?>`)
1
  • Perfectly fixed my issue of taking user input from a textarea! Thank you!
    – Ryan Ellis
    Mar 5, 2019 at 2:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.