Kazi Shahadat Hossain Kazi Shahadat Hossain - 1 year ago 77
PHP Question

how to use PHP variable in Javascript

I am developing a web application in php, and I've used javascript many times. Now I need to use a php variable in javascript.
For example, a variable generated from a SQL query return, or a session variable, which are needed in the javascript section.

What is the best way to use a php variable in javascript?

Answer Source

Maybe you could do something like:

  var a = <?php echo $a; ?>; //for numbers

That's one basic way of accomplishing what you want.

EDIT: As JiteshNK also pointed out, you could also do:

  var a = <?php echo json_encode($a); ?>; //safest solution
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download