If else php statement


If else php statement

如果有一个图像我希望它与内容一起显示,如果没有内容的图像,则不显示任何内容。目前,使用下面的代码,如果没有图像,那么我的页面上会出现一个损坏的图像。如果有一个图像,它工作得很好。任何建议都将不胜感激。非常感谢。

<?php if ($totalRows_rsPress > 0) {  ?>
<img class="img-responsive" src="images/<?php echo $row_rsPress['image']; ?>" alt="Image text" /> 
<?php } elseif ($totalRows_rsPress == 0) { return "" ; } ?>

试试这个:

<?php if ($totalRows_rsPress > 0 && isset($row_rsPress['image']) && strlen($row_rsPress['image']) > 0) {  ?>
<img class="img-responsive" src="images/<?php echo $row_rsPress['image']; ?>" alt="Image text" /> 
<?php } elseif ($totalRows_rsPress == 0) { return "" ; } ?>

检查图像是否存在:

if ( ! empty($row_rsPress['image']) ) {
    echo '<img class="img-responsive" src="images/' . $row_rsPress['image'] . '" alt="Image text" />';
}
<?php if($totalRows_rsPress > 0) : ?>
    <img class="img-responsive" src="images/<?=$row_rsPress['image'];?>"/>
<?php endif; ?>

没有必要发表其他声明。

在不知道totalRows_rsPress是什么的情况下,我无法告诉你为什么它可能会失败,如果条件为。。

尝试使用计数,即:

if (count($totalRows_rsPress) > 0) { 
echo <<< EOF
<img class="img-responsive" src="images/{$row_rsPress['image']}" alt="Image text" /> 
EOF;
}
<?php
    try{
         if ($totalRows_rsPress > 0) {
             echo "<img class=''img-responsive'' src=''images/$row_rsPress['image']'' alt=''Image text'' />"};
         } elseif ($totalRows_rsPress == 0) { return "" ; }    
    }
    catch(err) {
        //do nothing or do in case 004
    }

?>