如何在 PHP 中的字符串中使用 #
尝试为使用 jquery 的 WordPress 创建一个插件:
echo "$('#datepicker').datepicker({ ..... ";
# 正在作为注释工作,我尝试 \#
来阻止它,但这不起作用。有什么想法吗?
代码:
$dispWidget = $dispWidget.'<script type="text/javascript">';
$dispWidget = $dispWidget.'$(function() {";
$dispWidget = $dispWidget."$('#datepicker').datepicker({";
$dispWidget = $dispWidget."changeMonth: true,";
$dispWidget = $dispWidget."changeYear: true,";
Trying to create a plugin for wordpress that uses jquery:
echo "$('#datepicker').datepicker({ ..... ";
The # is working as a comment i tried \#
to stop it but that doesnt work. Any ideas?
Code:
$dispWidget = $dispWidget.'<script type="text/javascript">';
$dispWidget = $dispWidget.'$(function() {";
$dispWidget = $dispWidget."$('#datepicker').datepicker({";
$dispWidget = $dispWidget."changeMonth: true,";
$dispWidget = $dispWidget."changeYear: true,";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果错误是“解析错误:语法错误,意外的 T_VARIABLE”,则问题实际上与美元符号有关。
要解决此问题,请在 PHP 字符串中使用单引号,在 JavaScript 中使用双引号。
单引号对于性能也更好。
If the error is "Parse error: syntax error, unexpected T_VARIABLE" the problem is actually related to the dollar sign.
To fix this, use single quotes in your PHP strings and double quotes in your JavaScript.
Single quotes are better for performance as well.
实际发生的情况是这样的:
您打开一个单引号,然后关闭它(当您只是想向字符串添加一个时),然后添加散列,如下所示:
您想要的是这样的:
该单引号必须使用反斜杠转义才能被视为字符而不是结束引号。
What actually happens is this:
You open a single quote, then close it (when you juts wanted to add one to the string), and then add the hash, like this:
What you wanted was this:
That single quote has to be escaped with a backslash to be treated as a character instead of a closing quote.