2014-07-08 48 views
0

我想在回声中放置if语句,我不太确定如何去做。这里是回声:如何在echo中添加PHP 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> 
';} 

在第3行,我想换图像中的if语句:

if($productStatus = '0') { 

} 

什么是包裹在声明中形象的最佳方式是什么?谢谢!

回答

1

实际上,你可以结束控制的流程框一样,如果他们被打开同一PHP块外的语句。例如,这应该工作:

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

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

     ...HTML... 

    </div> 
<?php } ?> 

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

+0

完美,谢谢你的例子和资源! – user13286

0

使用数组来保存每个$ productStatus的CSS类(带背景图像)
快速高效。从HTML模式切换到PHP模式时,性能很热。这种方法消除了英特尔微代码中ifif性能的下降。

<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"> ... '; 

我也将使用4位色GIF图标并将其转换为Base64 MIME
确保页供应用gzip将会有很少或几乎没有惩罚的Base64编码。
如果您想坚持使用图像文件,请确保图像以最大缓存最大值进行投放。

background-image: url('data:image/gif;base64,R0lGODlhKAAoAK...');