1

I was testing a simple assignment of a variable using Heredoc, but when I try to echo it nothing shows up

<?php
$varable = <<<LINES
1st line
2nd line
LINES;
echo "<script>alert(\"$varable\")</script>";
?>

But when I try to run it with only 1 line, it works correctly:

<?php
$varable = <<<LINES
1st line
LINES;
echo "<script>alert(\"$varable\")</script>";
?>
Albert Einstein
  • 7,472
  • 8
  • 36
  • 71
Lenia
  • 11
  • 1
  • 1
    JavaScript does not support multi-line strings when using single or double quotes. There will be an error in your browser's console... _"Uncaught SyntaxError: Invalid or unexpected token"_ – Phil Nov 22 '18 at 04:38
  • The solution would be to use backticks, ie `alert(\`$varable\`)` or encode the newline, ie `echo '';` – Phil Nov 22 '18 at 04:56
  • Oh, thank you so much! – Lenia Nov 22 '18 at 05:05

0 Answers0