如何在 html 中回显变量?
我试图在一些现有代码中包含一个变量,但我的 PHP 技能非常基础。我环顾四周,找不到任何可以轻松应用的解决方案,尽管我确信它非常简单。
原始代码是:
<p class="ad-price"><?php if(get_post_meta($post->ID, 'price', true)) cp_get_price_legacy($post->ID); else cp_get_price($post->ID); ?></p>
我试图将其更改为(并且认为应该有效)的代码是:
<?php $yourtext = get_post_meta($post->ID, 'cp_pricing_period', TRUE);
echo "<p class="ad-price"><?php if(get_post_meta($post->ID, 'price', true)) cp_get_price_legacy($post->ID); else cp_get_price($post->ID); ?> $yourtext</p>"; ?>
这些更改显然是错误的,因为它会停止整个文件的工作。谁能告诉我我哪里做错了?
谢谢
I'm trying to include a variable within some existing code but my PHP skills are quite basic. I've looked around and can;t find anything I can reaidly apply as a fix for this although I'm sure it's pretty simple.
The original code is:
<p class="ad-price"><?php if(get_post_meta($post->ID, 'price', true)) cp_get_price_legacy($post->ID); else cp_get_price($post->ID); ?></p>
and the code I'm trying to change it to (and think should be working) is:
<?php $yourtext = get_post_meta($post->ID, 'cp_pricing_period', TRUE);
echo "<p class="ad-price"><?php if(get_post_meta($post->ID, 'price', true)) cp_get_price_legacy($post->ID); else cp_get_price($post->ID); ?> $yourtext</p>"; ?>
These changes are obviously wrong because it stops the whole file working. Can anyone shed light on where I'm getting this wrong?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你必须转义 html 中的双引号
you have to escape the double quotes in your html
两个问题。第一,您的 echo 语句中存在不匹配的引号:
PHP 会将这些额外的引号视为字符串终止,然后想知道这个
ad-price
指令是什么。这将是一个语法错误。同样,一旦您通过转义嵌入的引号对字符串进行排序:
您仍然会发现这不起作用。 PHP 不会将字符串中的
视为要执行的 PHP 代码。它位于字符串内,因此它将被视为字符串的一部分,并且 PHP 代码将回显给用户。在这种情况下,您可能会像这样编写整个内容:
Two problems. One, you've got mis-matched quotes within your echo statement:
PHP will see those extra quotes as terminating the string, and then wonder what this
ad-price
directive is. This'll be a syntax error.As well, once you get the string sorted out by escaping the embedded quotes:
you'll still end up with this not working. PHP will NOT see the
<?php ... ?>
within the string as PHP code to execute. it's within a string, so it'll be treated as part of the string and the PHP code will be echoed out to the user. In this case, you'd probably have write the whole thing more like this:在这里,您有一些语法错误(引号未转义并且标签错误地打开/关闭)。作为旁注,“扩展”括号使代码更具可读性,并且更容易发现错误。
Here, you have some syntax error (quotes not escaped and tags wrongly opend/closed). As a sidenote, "expanding" brackets makes your code more readable and it's easier to spot errors.
在此语句中,您需要在引号前添加反斜杠:
You need to put backslashes before the quotation marks in this statement: