I am trying to copy the string
$str = '
this
is
my
string';
to a JavaScript variable like so
var str1 = '<?php echo $str ?>';
but when I do, I get an error
Uncaught SyntaxError: Unexpected token ILLEGAL
If you are asking yourself why I’m doing this,
I create a <table> with PHP and insert it to the $str variable and want to later use it in a JavaScript function.
You should just use
json_encode()from http://www.php.net/manual/en/function.json-encode.phpExample:
It takes care of converting newlines to
\n, escaping any other special characters as necessary, preserve spaces, etc.Example output for your string: