admin管理员组文章数量:1356522
I'm trying to pass some variables from PHP to JS.
I saw here that for strings the following code would work:
var str = "<?php echo $x; ?>";
I've been using this:
var var1 = <?= $var1 ?>;
Which one should I use for int? the first version is always good? Is the second one just an old form? I'd be happy for a clarification and instruction on passing variables (dif types) from php to js in general.
Thanks!
I'm trying to pass some variables from PHP to JS.
I saw here that for strings the following code would work:
var str = "<?php echo $x; ?>";
I've been using this:
var var1 = <?= $var1 ?>;
Which one should I use for int? the first version is always good? Is the second one just an old form? I'd be happy for a clarification and instruction on passing variables (dif types) from php to js in general.
Thanks!
Share Improve this question edited May 23, 2017 at 12:19 CommunityBot 11 silver badge asked Jan 8, 2012 at 5:45 Lucy WeatherfordLucy Weatherford 5,54416 gold badges52 silver badges78 bronze badges 3- If it is in int, than you would not want the quotes, so you would remove them in the first code sample. – epascarello Commented Jan 8, 2012 at 5:48
- The second uses shorttags syntax, which is suggested not to be used by some (and there have been rumblings that PHP will drop default shorttag syntax being enabled at some point in the future, and some setups don't have it enabled, etc.). – Jared Farrish Commented Jan 8, 2012 at 5:50
-
1
This question has been asked so many times. The best and safest way to do it is
json_encode
. – igorw Commented Jan 8, 2012 at 13:32
6 Answers
Reset to default 6First of all, lets go over how to declare a variable in javascript.
There are many types of variables, but lets go over int
and string
. Strings have to have quotation marks around the. Int
's don't.
var int = 5;
var string = "hello";
Now when we are passing a variable from php to javascript, all we are doing is replacing the value after the equal sign with a variable declared in php. You mentioned the two ways for echoing a variable.
var str = "<?php echo $phpVariable; ?>";
var str = "<?= $phpVariable ?>";
If these variables were int
(which by the way includes any number, whether integer or not), there would be no quotation marks around the php open and close tags. Now, you said you prefer using the second method. I would really advise you not to. Even though it is a lot easier to type <?= $phpVariable ?>
than <?php echo $phpVariable; ?>
, the former one isn't supported on all servers, whereas the latter one is. You can use it if you want, but if you ever want to move to a server, you need to check if that syntex is allowed first.
Well, Lets say in PHP,
$php_int_variable = 5;
$php_string_variable = "Hello";
For getting it in javascript as like following,
var int = 5;
var string = "Hello";
You have to write,
var int = <?php echo $php_int_variable; ?>;
var string = "<?php echo $php_string_variable; ?> ";
For a general solution, use this:
var data = JSON.parse("<?php echo addslashes(json_encode($data)); ?>");
EVEN Anyone with problems , make the Next Way !
var str = "<?php echo $x; ?>";
this to string !
and
var int = parseInt('<?php echo $x; ?>');
this to Int!
var a = parseInt("10") + "<br>";
var b = parseInt("10.00") + "<br>";
var c = parseInt("10.33") + "<br>";
var d = parseInt("34 45 66") + "<br>";
var e = parseInt(" 60 ") + "<br>";
var f = parseInt("40 years") + "<br>";
var g = parseInt("He was 40") + "<br>";
var h = parseInt("10", 10)+ "<br>";
var i = parseInt("010")+ "<br>";
var j = parseInt("10", 8)+ "<br>";
var k = parseInt("0x10")+ "<br>";
var l = parseInt("10", 16)+ "<br>";
var n = a + b + c + d + e + f + g + "<br>" + h + i + j + k +l;
The two versions are equivalent. The 2nd version is enabled when the short_open_tag option is enabled in your php.ini. In php 5.4, the <?=
will be available even without short_open_tag enabled.
本文标签: javascriptHow to pass an integer value to JS that is in a PHP integer variableStack Overflow
版权声明:本文标题:javascript - How to pass an integer value to JS that is in a PHP integer variable? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743966918a2570038.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论