3

I'm having a problem trying to use a PHP variable within JavaScript. I keep getting the following message.

"Invalid or unexpected token. Message: Undefined variable: example."

I'm unsure why example is being undefined, as it is defined within the php code. Here is my code:

<?php
    $example = '2';
?>
<script type="text/javascript">
    var php_var = "<?php echo json_encode($example); ?>";
</script>

Does anyone have any suggestions? I have also tried the following javascript that results in the same problem:

<script type="text/javascript">
    var php_var = "<?php echo $example; ?>";
</script>
4
  • 1
    you missed semi colon after $example = '2' that might be problem. Commented Jan 10, 2017 at 11:43
  • @SoniyaReddy I think you missed what he is trying to do. $example should be output as var php_var = "2"; Commented Jan 10, 2017 at 11:46
  • I deleted my answer because I realized I had read (skimmed) too fast. Commented Jan 10, 2017 at 11:52
  • json_encode will add double quotes, making var php_var = ""2""; Having said that var php_var = "<?php echo $example; ?>"; worked as expected for me. Commented Jan 10, 2017 at 11:53

3 Answers 3

1

Firstly, your original code has a syntax error: $example = '2' needs a semicolon. Secondly, the next piece of code is just assigning the string <?php echo $example; ?> to the JavaScript variable php_var where the $example PHP variable is first substituted. The $example variable should be initiated properly first, however, for this to work.

As a separate note: JS cannot execute PHP directly -- only a PHP server can do so. What you're most likely trying to do is this:

<?php
    $example = '2';
?>
<script type="text/javascript">
    var php_var = '<?php echo $example ;?>';
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for the help! I added this in my footer.php and it worked. For some strange reason, $example when created on my body page is not going to the footer.
I'm confused as to what is different in your answer and the question? I cannot see any difference except ' being changed to ". But I doubt that is the actual solution... Both single and double quotes don't work for me. Why could that be? UPDATE: they actually do print the php var, but because of the error, the rest of the javascript is broken.
this is assigning php_var to a literal string. This didn't work for me. i am using chrome.
1

This should work, use single quotes

<?php
  $example = '2';
?>
  <script type="text/javascript">
      var php_var = '<?php echo  $example; ?>';
  </script>

Comments

0

Tried and working both variant:

<?php
    $example = '2';
?>
<script type="text/javascript">
    var php_var = <?php echo json_encode($example); ?>;
    console.log(php_var);
</script>

<script type="text/javascript">
    var php_va = "<?php echo $example; ?>";
    console.log(php_va);
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.