Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 条件语句中的回显图像变量_Php_Wordpress - Fatal编程技术网

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))是否存在,因为如果该文件在服务器上不可用,那么它将为您提供错误信息