简体   繁体   English

在echo语句中使用双引号和单引号

[英]Use double and single quotes in an echo statement

I am struggling to work out how I can use both double and single quotes in an echo statement without breaking open the echo. 我正在努力弄清楚如何在echo语句中同时使用双引号和单引号,而又不会破坏echo。

For example, I want to create a link that outputs like this: 例如,我想创建一个输出如下的链接:

<a href="http:/www.blah.com" class="read-more" onClick="_gaq.push(['_trackEvent', 'External Link', 'Buy Link', 'Item name']);">Read this post...</a>

However I want to output PHP variables in place of the href and as one of the parameters in the onClick 但是我想输出PHP变量代替hrefonClick中的参数之一

The href part is easy, I can break open the PHP echo and concat the variable in: href部分很简单,我可以打开PHP echo并在以下位置连接变量:

echo '<a href="'.$link.'" class="read-more">Read this post...</a>';

But i'm stuck when it comes to the parameters inside the onClick, because I can't include them as the use of ' breaks open my echo: 但是当涉及onClick内部的参数时,我会陷入困境,因为我不能将它们包括在内,因为使用'会打断我的回声:

echo '<a href="<?php echo the_permalink(); ?>" class="read-more" onClick="_gaq.push(['_trackEvent', 'External Link', 'Buy Link', ''. echo $pattern_name .'']);">Read this post...</a>';

So this code above breaks when it gets to the 'trackEvent' part because the single quotes break it open. 因此,以上代码在到达'trackEvent'部分时会中断,因为单引号会将其打开。

What can I do? 我能做什么?

反斜杠( 转义字符 )是您最好的朋友:

echo '<a href="'.the_permalink().'" class="read-more" onClick="_gaq.push([\'_trackEvent\', \'External Link\', \'Buy Link\', \''. $pattern_name .'\']);">Read this post...</a>';

You have a few options, but personally i find it ugly code when using something like echo ' 您有一些选择,但是就使用echo'之类的东西而言,我个人觉得它很丑陋

I would suggest something more along these lines (escaping) 我会建议一些类似的方法(转义)

$pattern_name = "something";
$linkhref = someFunction();
echo "<a href='{$linkhref}' class='read-more' onclick='_gaq.push([\"_trackEvent\", \"External Link\", \"Buy Link\", \"{$pattern_name}\"]);'>Read this post...</a>";

Of this 这个的

$pattern_name = "something";
echo "<a href='".someFunction()."' class='read-more' onclick='_gaq.push([\"_trackEvent\", \"External Link\", \"Buy Link\", \"{$pattern_name}\"]);'>Read this post...</a>";

Just a personal preference 只是个人喜好

Results in... 结果是...

<a href='index.php' class='read-more' onclick='_gaq.push(["_trackEvent", "External Link", "Buy Link", "something"]);'>Read this post...</a>

Using complex (curly) syntax, http://php.net/manual/en/language.types.string.php#language.types.string.parsing.complex 使用复杂的(curly)语法, http://php.net/manual/en/language.types.string.php#language.types.string.parsing.complex

Aside: your HTML code will be rather more readable if you do this: 顺便说一句:如果您这样做,您的HTML代码将更具可读性:

<a
    href="http:/www.blah.com"
    class="read-more"
    onClick="_gaq.push(['_trackEvent', 'External Link', 'Buy Link', 'Item name']);"
>Read this post...</a>

Each attribute gets its own line, and thus is easy to find, and horizontal scrolling in your editor becomes much less necessary. 每个属性都有其自己的行,因此易于查找,并且在编辑器中进行水平滚动变得不必要了。 The great thing is that this is perfectly valid HTML. 很棒的是,这是完全有效的HTML。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM