Heredoc 在 JavaScript 中转义 PHP 变量
我有以下代码:
<?php
if ($zweck == "buchhaltung") {
echo <<<EOF
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#$grid_name").jqGrid({
url: 'modules/mod_jqgrid/ex_get3.php?tb=$tb'
.....
</script>
EOF;
};
?>
...似乎无法正确渲染。 我们不能像在倒数第二行中那样在 JavaScript 代码的 heredoc 中使用 PHP 变量吗?
在最后一行,我在 PHP 变量 $tb 周围使用“'”。这个语法正确吗?
以下代码位于定界文档中作为 JavaScript 代码:
dataInit:function(el){
$(el).datepicker({dateFormat:'dd.mm.yy'});
},
defaultValue: function(){
// Maybe PHP "thinks" that $(el) is a PHP variable?
var currentTime = new Date();
I have the following code:
<?php
if ($zweck == "buchhaltung") {
echo <<<EOF
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery("#$grid_name").jqGrid({
url: 'modules/mod_jqgrid/ex_get3.php?tb=$tb'
.....
</script>
EOF;
};
?>
... which does not seem to render properly.
Can't we use PHP variables in heredoc in JavaScript code like I use it on the second last line?
On the last line I use " ' " around the PHP variable $tb. Is this syntax correct?
The following code is inside the heredoc as JavaScript code:
dataInit:function(el){
$(el).datepicker({dateFormat:'dd.mm.yy'});
},
defaultValue: function(){
// Maybe PHP "thinks" that $(el) is a PHP variable?
var currentTime = new Date();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
变量扩展是在定界字符串中执行的,所以这不是问题。您提供的代码应该可以正常工作;如果没有,也许还有其他问题?您到底是什么意思“无法正确渲染”?
Variable expansion is performed in heredoc strings, so that's not the problem. The code you give should work fine; if it does not, maybe something else is amiss? What exactly do you mean "does not render properly"?
让您明白:
没有必要转义任何内容:只需将 JavaScript 代码与 PHP 分开即可。
以每种语言的母语方式使用它。
To get you idea:
There isn't any need to escape anything: Just separate your JavaScript code from PHP.
Use each language in its native way.