If statement 如何在echo中添加PHP if语句?

If statement 如何在echo中添加PHP if语句?,if-statement,echo,If Statement,Echo,我想在echo中放置一个if语句,但我不太确定如何执行。这是回音: if(!$hideProduct) { echo ' <div class="gearBorder"> <img title="Sold Out" alt="Sold Out" class="soldOut" src="soldout.png" />':"").' <div class="gearInfo"> &

我想在echo中放置一个if语句,但我不太确定如何执行。这是回音:

if(!$hideProduct) {         
echo '
    <div class="gearBorder">
        <img title="Sold Out" alt="Sold Out" class="soldOut" src="soldout.png" />':"").'
        <div class="gearInfo">
            <h4>' . $productName . '</h4>
            <p class="gearDesc">'. $productDescription .'</p>
            <p class="cost">$' . $productPrice . '</div>
        </div>  
    </div>
';}

在这句话中,最好的包装方式是什么?谢谢

您实际上可以结束控制流块,就像if语句在打开它们的同一PHP块之外一样。例如,这应该起作用:

<?php if (!$hideProduct) { ?>
    <div class="gearBorder">

        <?php if ($productStatus == '0') { ?>
            <img title="Sold Out" ... />
        <?php } ?>

        ...HTML...

    </div>
<?php } ?>

…HTML。。。

如果不喜欢大括号,还可以分别用冒号(:)和endif替换它们。有关更多信息,请参阅。

使用数组保存每个$productStatus的CSS类(带有背景图像)
快速高效。当您从HTML模式切换到PHP模式时,会出现性能问题。此方法消除了英特尔微代码中if elseif性能的影响

<style type="text/css">
.soldout{width:40px;height:40px;background-image: url('soldout.png');}
.backorder{width:40px;height:40px;background-image: url('backorder.png');}
.instock{width:40px;height:40px;background-image: url('instock.png');}
</style>

$icon = array(' class="soldout" ',' class="backorder" ',' class="instock" ');.

echo '<div class="gearBorder"><div ' . $icon[$productStatus] . '></div><div class="gearInfo"> ... ';

太好了,谢谢你的例子和资源!
<style type="text/css">
.soldout{width:40px;height:40px;background-image: url('soldout.png');}
.backorder{width:40px;height:40px;background-image: url('backorder.png');}
.instock{width:40px;height:40px;background-image: url('instock.png');}
</style>

$icon = array(' class="soldout" ',' class="backorder" ',' class="instock" ');.

echo '<div class="gearBorder"><div ' . $icon[$productStatus] . '></div><div class="gearInfo"> ... ';
background-image: url('data:image/gif;base64,R0lGODlhKAAoAK...');