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

How can I pass a PHP variable value to javascript?

share|improve this question
1  
How can I install a video card on my PC? – mdrg Feb 7 '11 at 11:09
2  
@mdrg: The question is clean, simple and answerable! – Goran Jovic Feb 7 '11 at 11:11
1  
wtf at closing this question it's 100% fine – Tom Gullen Feb 7 '11 at 11:12
1  
I don't understand why people are voting to close this question - does anybody want to explain? – Nathan Long Feb 7 '11 at 11:12
1  
@AlexanderMP they were the first two matches I got, but if they are not fine, a better search may provide better results: stackoverflow.com/questions/168214/… – mdrg Feb 7 '11 at 11:22
show 4 more comments

closed as not a real question by aaronasterling, Joel Etherton, Boris Guéry, AlexanderMP, Christopher Orr Feb 7 '11 at 20:53

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

4 Answers

// your PHP file which outputs HTML
<script type="text/javascript">
    var jsVariable = <?php echo json_encode($phpVariable); ?>
</script>
share|improve this answer
$phpVariable="Welcome to this website"; ... – AlexanderMP Feb 7 '11 at 11:16
Yes, it's not safety. It's just prototype. – KomarSerjio Feb 7 '11 at 11:25

Never pass a variable like @Goran Jovic or @KomarSerjio suggested. It's asking for a trouble. Use JSON (json_encode() function) that will prepare and filter data.

$data = json_encode(array(
    'myVar' => 123,
    'anotherVar' => 'Hello "world"'
));

...

var data = <?php echo $data ?>;

alert(data.anotherVar);

This guarantees that no error will occur.

share|improve this answer
thx for ur answers... but i'm using it in client side.. – Dhinesh.B Feb 7 '11 at 11:19
you can't use PHP client-side. PHP is a server-side language. You must be misunderstanding the answer. PHP will output real literals which work for Javascript, and the client will work with pure Javascript. – AlexanderMP Feb 7 '11 at 11:21
KomarSerjio's answer was corrected, because it has the highest amount of votes. So one should use my suggestion, yours or KomarSerjio's, as they all almost the same. – AlexanderMP Feb 7 '11 at 11:30

In your PHP code, which outputs HTML, in the <head> section, before using any of the variables, use any appropriate method:

<script type="text/javascript">
    var someStringVariable = '<?php echo str_replace("'","\\'",$yourStringPHPVar);?>';
    var someObjectOrArrayVariable = <?php echo json_encode($phpArray); ?>;
    var someNumericVariable = <?php echo $yourNumericPHPVar); ?>;
</script>
share|improve this answer

you need to understand the meaning of "server-side" and "client-side".

basically, the php is "generating" the output for the browser (and the js is just part of that output)

so in order to "pass variable from php to js" all you need to do is just "echo" the js variable:

<script>
<?
//this is the php part
echo "var myJsVar='hello';"
?>

var mySecondJsVar="<?=$phpVar?>";
</script>
share|improve this answer

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