0

I'm a very noob to PHP.

Below is my code

<script>
 let x = <?php echo "AB"; ?>
 console.log(x)
</script>

Log says that 'ABconsole is not defined'.

Is there anything else I missed??

Thanks, in advance.

Boolhwi
  • 39
  • 5
  • "_Is there anything else I missed??_" Yes, the quotes in JS – brombeer Apr 07 '22 at 08:33
  • let x = '' – TheNick Apr 07 '22 at 08:34
  • 2
    Does this answer your question? [Pass a PHP string to a JavaScript variable (and escape newlines)](https://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-and-escape-newlines) – Gary Thomas Apr 07 '22 at 08:35
  • for you the console.log is an other instruction but not for the computer , you need an Instruction separation `;` , resultat -> `let x = ; ` , you can remove the separator in the php, with the `?>` you are tell him your instruction is end so `let x = ; ` – WolfCode Apr 07 '22 at 08:38
  • What exactly is the use of printing the string `AB` via PHP? Why not just do that in JS? – brombeer Apr 07 '22 at 08:47
  • @WolfCode The missing `;` is not the problem here, it's the missing quotes. Adding `;` would still error "Uncaught ReferenceError: AB is not defined" – brombeer Apr 07 '22 at 08:49
  • @brombeer Exactly what I want, it was quote problem. Adding ';' doesn't work in this problem. Also, regardless with 'encode_json' method. – Boolhwi Apr 08 '22 at 01:51
  • @brombeer Simply printing string is not my goal. When I check php variable with js, console.log() doesn't work while console.log() work. I just wanted to print pretty like console.log(). – Boolhwi Apr 08 '22 at 01:56

0 Answers0