Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

So far I know two ways to pass php variables to javascript. One is by using

<script>  
      $(document).ready(function()
          phpvalue=$("#hiddeninput").val()

      })
  </script>

<input type="hidden" id="hiddeninput" value="phpvalue">

And the other one is by having for example a button and using onclick

    <script>
       function clickf(phpvalue) {

       }
    </script>

<input type="submit" onclick="clickf(<?php echo phpvalue ?>)">

All of them work great but:

1) Is there any other way that I'm missing?

2) Which one is the "best"?

3) Any chance that I can use inside the script or the external js ?

share|improve this question

marked as duplicate by ChrisF Apr 11 at 21:51

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5 Answers

up vote 8 down vote accepted
<script>  
    $(document).ready(function()
        var phpvalue = <?php echo $var; ?>
    });
</script>
share|improve this answer
So that answers my third question. But can I use this in an external js? Thank you – viper Jan 6 '12 at 13:22
won't work in a .js file, but you can just put all you javascript in a php file and pass your php values to javascript variables and import that php file like you normally would a js file. <script src="script.php"></script> – user990156 Jan 6 '12 at 13:25
I thought of that also but this would require a header like this right? header(Content-Type: text/javascript) – viper Jan 6 '12 at 13:32
No, that's not necessary – user990156 Jan 6 '12 at 13:46

Like others already answered, just put a PHP tag whereever you would place the JS value, like var foo = <?= $php_value ?>; or var foo = <?php echo $php_value ?>;.

If you want to use this in an external JavaScript file, you have to make sure .js files get parsed by PHP. If this is not an option for you (for example, your host doesn't allow it), I suggest you set the values in a <script> tag inside your <head> and then just reference thos variables from your external JavaScript. In that case, I would strongly suggest you namespace them like var app_vars = { foo: <?= $bar ?> } in order to not clutter the global object.

Another way would be to retreive the values via Ajax. But the viability of this approach depends on your use case.

And another hint: if you want to pass multiple variables or arrays, there's JSON baked into PHP since version 5.2:

<?php
    $my_complex_var = array(
        'array' => array('foo', 'bar'),
        'val2' => 'hello world'
    );
?>
<script>
    var my_complex_js_var = <?= json_encode($my_complex_var) ?>
</script>
share|improve this answer
That's very nice thank you. One question though why would I need to namespace the var? I didn't get that! – viper Jan 6 '12 at 13:34
Also as mentioned earlier by @garvey I can use .php instead of .js right? – viper Jan 6 '12 at 13:35
Namespacing helps you keep your global object clean. And keeping the GO clean is just best practice for a thousand reasons. If this doesn't make too much sense to you, I'd suggest watching some of Douglas Crockford's talks. Just do a Google Video search for his name. And yeah, actually you can just use .php as the file extension and you can mix JS and PHP in there. Or, even better, .js.php. This way you'll still know what's going on in these files. – Rudolph Gottesheim Jan 6 '12 at 13:42
<script>

var javascript_variable = <?php echo $php_variable; ?>;

</script>
share|improve this answer

Instead of assigning values to hidden inputs, you could just generate JavaScript directly:

$script = <<<EOT
<script>
var phpvalue = $phpvalue;
</script>
EOT;
echo $script;
share|improve this answer

for the top example you do not need to use val you could use any attr you like. For example

phpvalue="myvalue"

then within jquery

$("#hiddeninput").attr("phpvalue");
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.