0

my php code has a value of 2 digits like

 $var1 = 16; 
 $var2 = 24;
 $var3 = 31;

but when i used those value and put it in my javascript like for example

var var1 = <?php echo $var1; ?>
var var2 = <?php echo $var2; ?>
var var3 = <?php echo $var3; ?>
console.log(var1+"-"+var2+"-"+var3)

the output is **1-2-3** instead of **16-24-31** it only get the first digit i even put window.onloadfunction to make sure to load the php and html first before the script.

asked Sep 24, 2013 at 9:22
16
  • 3
    i even put window.onload function to make sure to load the php and html first before the script. <-- this is EPIC. Commented Sep 24, 2013 at 9:25
  • If you view the source of the page, do the same (1-2-3) values show up in the var var1 = etc.. part of the code? Commented Sep 24, 2013 at 9:25
  • 'to make sure to load the php and html first before the script' - PHP executes on the server, upon page request: always before the return javascript Commented Sep 24, 2013 at 9:25
  • What source does it generate? Also, I can't imagine there would be * characters in your output. Commented Sep 24, 2013 at 9:25
  • 2
    not clear from your snippet, but it looks like you are missing semicolons Commented Sep 24, 2013 at 9:25

2 Answers 2

1

The problem is a result of two things:

  1. You are not ending your JS statements with semicolons.
  2. PHP eats up one newline character if it follows the closing tag ?>.

This means that your JS code ends up being something like

var var1 = 16var var2 = 24var3 = 31

which is not what you expect (actually the above is a syntax error; if the code compiles in your case it might be a result of the example being not identical to the code that runs).

The proper solution is to terminate each statement with a semicolon:

var var1 = <?php echo $var1; ?>;
var var2 = <?php echo $var2; ?>;
var var3 = <?php echo $var3; ?>;

What would also work (although I recommend to avoid it) is to put some extra newlines after each assignment and let ASI take over:

var var1 = <?php echo $var1; ?>
var var2 = <?php echo $var2; ?>
var var3 = <?php echo $var3; ?>

As an aside, straight echo of the variables works in this case because they are numeric. In general, the proper way to transfer variables to JS is through json_encode:

var var1 = <?php echo json_encode($var1); ?>;
answered Sep 24, 2013 at 9:41
Sign up to request clarification or add additional context in comments.

2 Comments

this solve the problem as using json_encode to output the variable even if it has white spaces thank you for this
@DevfaR: But your example variables don't contain whitespace. Why?
0

try this : It worked for me

<?php
 $var1 = 16; 
 $var2 = 24;
 $var3 = 31;
?>
<script>
var var1 = <?php echo $var1; ?>;
var var2 = <?php echo $var2; ?>;
var var3 = <?php echo $var3; ?>;
alert(var1+"-"+var2+"-"+var3);
console.log(var1+"-"+var2+"-"+var3);
</script>
answered Sep 24, 2013 at 9:28

1 Comment

well it's the same problem i encounter

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.