如何在 html 中回显变量?

发布于 2024-11-15 15:15:51 字数 664 浏览 1 评论 0原文

我试图在一些现有代码中包含一个变量,但我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

岁月静好 2024-11-22 15:15:51

你必须转义 html 中的双引号

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>"; ?>

you have to escape the double quotes in your html

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>"; ?>
千と千尋 2024-11-22 15:15:51

两个问题。第一,您的 echo 语句中存在不匹配的引号:

echo "<p class="ad-price"><?php if(get_post_meta($post->ID, 'price', true))  etc....
               ^-here   ^--here

PHP 会将这些额外的引号视为字符串终止,然后想知道这个 ad-price 指令是什么。这将是一个语法错误。

同样,一旦您通过转义嵌入的引号对字符串进行排序:

echo "<p class=\"ad-price\">etc...."

您仍然会发现这不起作用。 PHP 不会将字符串中的 视为要执行的 PHP 代码。它位于字符串内,因此它将被视为字符串的一部分,并且 PHP 代码将回显给用户。在这种情况下,您可能会像这样编写整个内容:

echo '<p class="ad-price">';
if(get_post_meta($post->ID, 'price', true)) {
     echo cp_get_price_legacy($post->ID);
} else { 
     echo cp_get_price($post->ID);
}
echo " $yourtext</p>";

Two problems. One, you've got mis-matched quotes within your echo statement:

echo "<p class="ad-price"><?php if(get_post_meta($post->ID, 'price', true))  etc....
               ^-here   ^--here

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:

echo "<p class=\"ad-price\">etc...."

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:

echo '<p class="ad-price">';
if(get_post_meta($post->ID, 'price', true)) {
     echo cp_get_price_legacy($post->ID);
} else { 
     echo cp_get_price($post->ID);
}
echo " $yourtext</p>";
oО清风挽发oО 2024-11-22 15:15:51

在这里,您有一些语法错误(引号未转义并且标签错误地打开/关闭)。作为旁注,“扩展”括号使代码更具可读性,并且更容易发现错误。

<?php
$yourtext = get_post_meta($post->ID, 'cp_pricing_period', TRUE); 

echo "<p class=\"ad-price\">";
if(get_post_meta($post->ID, 'price', true)) 
{
 cp_get_price_legacy($post->ID);
}
else 
{
  cp_get_price($post->ID); 
}

echo $yourtext."</p>"; 
?>

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.

<?php
$yourtext = get_post_meta($post->ID, 'cp_pricing_period', TRUE); 

echo "<p class=\"ad-price\">";
if(get_post_meta($post->ID, 'price', true)) 
{
 cp_get_price_legacy($post->ID);
}
else 
{
  cp_get_price($post->ID); 
}

echo $yourtext."</p>"; 
?>
以歌曲疗慰 2024-11-22 15:15:51

在此语句中,您需要在引号前添加反斜杠:

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>"; ?>

You need to put backslashes before the quotation marks in this statement:

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>"; ?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文