Php 条件语句中的回显图像变量
我试图在条件语句中显示图像 如果没有条件,我可以简单地做:Php 条件语句中的回显图像变量,php,wordpress,Php,Wordpress,我试图在条件语句中显示图像 如果没有条件,我可以简单地做: <div> <img src="<?php echo($img); ?>" /> </div> " /> 但我有这样的情况: <?php if ($img ) { echo "image exists"; // Show image } else { echo "no image..";
<div>
<img src="<?php echo($img); ?>" />
</div>
" />
但我有这样的情况:
<?php
if ($img ) {
echo "image exists";
// Show image
} else {
echo "no image..";
}
?>
我正在使用wordpress,$img
包含一个url。
感谢您的帮助!谢谢!请尝试:
<?php if !empty($img) { // check if $img not empty ?>
<div> <!-- display image -->
<img src="<?php echo($img); ?>" />
</div>
<?php } else { // if $img empty 'no image' show message
echo "no image..";
}
?>
" />
您可以像
if (!empty($img) && file_get_contents($img) ) {
echo "image exists";
// Show image
} else {
echo "no image..";
}
您必须检查(file_get_contents($img))或(file_exists($img))是否存在,因为如果该文件在服务器上不可用,那么它将为您提供错误信息