开发者

How to Use a # in string in PHP

开发者 https://www.devze.com 2023-03-01 14:35 出处:网络
Trying to create a plugin for wordpress that uses jquery: echo \"$(\'#datepicker\').datepicker({ ..... \";

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').datepi开发者_开发百科cker({";
$dispWidget = $dispWidget."changeMonth: true,";
$dispWidget = $dispWidget."changeYear: true,";


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.

echo '$("#datepicker").datepicker({ ..... ';

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:

'..stuff..'#other stuff'

What you wanted was this:

'..stuff..\'#other stuff'

That single quote has to be escaped with a backslash to be treated as a character instead of a closing quote.

0

精彩评论

暂无评论...
验证码 换一张
取 消