将 PHP 变量打印到 JavaScript 变量中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15433894/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Printing PHP variables into JavaScript variables
提问by Compy
Yes I know this question gets asked a lot, but I'm fairly new to JS and I need to use a php variable in some JS. I'm more then aware that PHP is executed server side and JS is client side however other people claim that this works.
是的,我知道这个问题经常被问到,但我对 JS 还很陌生,我需要在某些 JS 中使用 php 变量。我更清楚 PHP 是在服务器端执行的,而 JS 是客户端,但是其他人声称这是有效的。
I've got a PHP variable called "test1" that I want to log to the JS console (for instance):
我有一个名为“test1”的 PHP 变量,我想将其登录到 JS 控制台(例如):
<?php
$test1 = '1';
print '
<script type="text/javascript">
var carnr;
carnr = "<?php print($test1); ?>"
console.log(carnr);
</script>';
?>
What this does is print " " to the JS console. Not exactly what I was hoping for.
这样做是将“”打印到 JS 控制台。不完全是我所希望的。
Now this may not even be doable and I may have to pass the variable off the page and back in again with AJAX, but I'd rather have a quick and easy solution if there is one available!
现在这甚至可能不可行,我可能不得不将变量从页面中传递出来,然后用 AJAX 再次传入,但如果有可用的解决方案,我宁愿有一个快速简便的解决方案!
Any help is appreciated.
任何帮助表示赞赏。
回答by Gopinagh.R
You could do this.
你可以这样做。
<script>
var JSvar = "<?= $phpVar ?>";
</script>
The PHP will be parsed and the value of $phpVar will become the value of JSvar whatever.
PHP 将被解析并且 $phpVar 的值将成为 JSvar 的值。
Make sure you encode phpVar properly. For example, if phpVar contains a double quote, you'll end up with a broken JS
确保正确编码 phpVar。例如,如果 phpVar 包含一个双引号,你最终会得到一个损坏的 JS
回答by MKV
Use this no need to give "" => change to '.$test1.'..
使用此无需将 "" => 更改为 '.$test1.'..
<?php
$test1 = '1';
print '
<script type="text/javascript">
var carnr;
carnr = "'.$test1.'"
console.log(carnr);
</script>';
?>
回答by til_b
try
尝试
<?php $test1 = '1'; ?>
<script type="text/javascript">
var carnr;
carnr = "<?php print($test1); ?>"
console.log(carnr);
</script>
Generally, it is better to not print static stuff with php, but to have static (that is unchanging) stuff directly in HTML and only use PHP on the parts that really need it.
通常,最好不要使用 php 打印静态内容,而是直接在 HTML 中包含静态(即不变的)内容,并且仅在真正需要它的部分使用 PHP。
回答by idmean
You made a mistake do it so:
你犯了一个错误这样做:
<?php
$test1 = '1';
echo '<script type="text/javascript"> var carnr; carnr = "'.$test1.'" console.log(carnr)</script>';
?>
回答by Major Productions
Since you're writing your JS with your PHP, you can simply do:
由于您使用 PHP 编写 JS,因此您可以简单地执行以下操作:
$test1 = "blah";
echo "<script type=\"text/javascript\">console.log($test1);</script>";
回答by LordShigi
You are already in open php tag. When printing the line just append the variable to the output by using a dot.
您已经在打开的 php 标签中。打印该行时,只需使用点将变量附加到输出。
Example:
例子:
print 'variable1 '.$variable1.' is now printed';

