+ 1

Can a PHP variable be passed to Javascript?

Is it possible to use a variable from a PHP script in Javascript and vice versa. Can variables be passed between the two languages?

12th Jul 2019, 7:06 PM
Vince Warner
Vince Warner - avatar
2 Answers
+ 4
Something like that : <?php echo "let foo =" +$bar; ?> And put this in between script tags. Php is evaluated before Javascript, so it works. To pass data from js to php, use ajax!
12th Jul 2019, 7:10 PM
Drax
Drax - avatar
0
On a side note, there are other way around this problem : you could use cookies, you could use get variable..
12th Jul 2019, 7:24 PM
Drax
Drax - avatar