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.

DevfaR
  • 1,588
  • 1
  • 9
  • 17
  • 3
    *i even put `window.onload` function to make sure to load the php and html first before the script.* – Niccolò Campolungo Sep 24 '13 at 09: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? – Novocaine Sep 24 '13 at 09: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 – George Sep 24 '13 at 09:25
  • What source does it generate? Also, I can't imagine there would be `*` characters in your output. – Bart Sep 24 '13 at 09:25
  • 2
    not clear from your snippet, but it looks like you are missing semicolons – Elen Sep 24 '13 at 09:25
  • View the source code JavaScript and tell us what those variables look like. – Wayne Whitty Sep 24 '13 at 09:26
  • @Elen: [ASI](http://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi) – Jon Sep 24 '13 at 09:26
  • the value i get from the source code is the same in php – DevfaR Sep 24 '13 at 09:29
  • try typecasting as an integer, e.g. `var var1 = ` you might need to do this in the javascript too but I am unsure how - maybe declare `int` instead of `var`? – verbumSapienti Sep 24 '13 at 09:31
  • @Jon his statement reads like: `var var1 = var var2 = var var3 =` - if php echo's nothing – Elen Sep 24 '13 at 09:33
  • @verbumSapienti JavaScript is a dynamic language, there are no implicit casting operations available as in C#, and the variables **have to be declared using var**(there are no types like *int*, *string*, etc). – Niccolò Campolungo Sep 24 '13 at 09:34
  • @Elen: Why on earth would PHP echo nothing? Is there a good reason to make this assumption? – Jon Sep 24 '13 at 09:35
  • @Jon yes always check your variables. – Elen Sep 24 '13 at 09:36
  • @Elen it is not as you said, otherwise the compiler would have thrown an exception. Follow the link Jon provided you, in JS semicolons are a messy problem. – Niccolò Campolungo Sep 24 '13 at 09:36
  • is there any factor that white spaces in php may causes the problem because some variable has white space? – DevfaR Sep 24 '13 at 09:38
  • @Elen: I have no idea what that's supposed to mean. The variables are definitely assigned in the example, *and* even if they were not there's no chance the output would be 1-2-3. – Jon Sep 24 '13 at 09:45

2 Answers2

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); ?>;
Community
  • 1
  • 1
Jon
  • 396,160
  • 71
  • 697
  • 768
  • this solve the problem as using json_encode to output the variable even if it has white spaces thank you for this – DevfaR Sep 24 '13 at 09:48
  • @DevfaR: But your example variables don't contain whitespace. Why? – Jon Sep 24 '13 at 09:50
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>
Prasanth Bendra
  • 26,567
  • 8
  • 48
  • 67