php 里面的 div echo
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15501030/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
div inside php echo
提问by LifeIsShort
i have this:
我有这个:
<?php
if ( ($cart->count_product) > 0) {
print $cart->count_product;
} else {
print '';
}
?>
and i need to put print $cart->count_productinside of <div class="my_class"></div>
我需要把print $cart->count_product里面<div class="my_class"></div>
I tried different ways, but i'm missing something in syntax. I'll be glad if someone could help.
我尝试了不同的方法,但我在语法上遗漏了一些东西。如果有人可以提供帮助,我会很高兴。
回答by BenM
You can do the following:
您可以执行以下操作:
echo '<div class="my_class">';
echo ($cart->count_product > 0) ? $cart->count_product : '';
echo '</div>';
If you want to have it inside your statement, do this:
如果您想将其包含在您的语句中,请执行以下操作:
if($cart->count_product > 0)
{
echo '<div class="my_class">'.$cart->count_product.'</div>';
}
You don't need the elsestatement, since you're only going to output the above when it's truthy anyway.
您不需要该else语句,因为无论如何您只会在它为真时输出上述内容。
回答by Edwin Alex
Try this,
尝试这个,
<?php if ( ($cart->count_product) > 0) { ?>
<div class="my_class"><?php print $cart->count_product; ?></div>
<?php } else {
print '';
} ?>
回答by ATaylor
Just wrap it around then.
那就把它包起来吧。
<?php
if ( ($cart->count_product) > 0)
{
echo "<div class='my_class'>";
print $cart->count_product;
echo "</div>";
}
?>
回答by UnholyRanger
You can do this:
你可以这样做:
<div class"my_class">
<?php if ($cart->count_product > 0) {
print $cart->count_product;
} else {
print '';
}
?>
</div>
Before hitting the div, we are not in PHP tags
在点击 之前div,我们不在 PHP 标签中
回答by believe me
You can also do this,
你也可以这样做,
<?php
if ( ($cart->count_product) > 0) {
$print .= "<div class='my_class'>"
$print .= $cart->count_product;
$print .= "</div>"
} else {
$print = '';
}
echo $print;
?>
回答by cowls
You can use the below sample, also you dont need the else clause to print nothing!
您可以使用以下示例,也不需要 else 子句来打印任何内容!
<?php if ( ($cart->count_product) > 0) { ?>
<div class="my_class">
<?php print $cart->count_product; ?>
</div>
<?php } ?>

