php foreach 语句拉取图片,如果 none 找到替换为保存图片

php foreach statement to pull images, if none found replace with holding image

我正在尝试 运行 在某些图像上执行以下 foreach,它不喜欢我的声明:

<?php foreach ($images as $image) {
    if (count($images) > 1) {
        '<img src="'.$image.'" class="slide">
         <a href="#" class="zoom"><i class="icon-popup"></i></a>'
    } else {
        '<img class="slide" src="no-image-large.jpg">';
    }
endforeach ?>

作为非php大师,我哪里做错了?

您的代码是 correct.but 您需要关闭 foreach 循环 properly.may 这对您有帮助。

<?php foreach ($images as $image) {
if (count($images) > 1) {
    '<img src="'.$image.'" class="slide">
     <a href="#" class="zoom"><i class="icon-popup"></i></a>'
} else {
    '<img class="slide" src="no-image-large.jpg">';
}
} ?>

这里有一些事情。

首先,您混合了 foreach 语法。要么

  1. foreach($a as $b) { code }
  2. foreach($a as $b): code endforeach;

此外,您需要将字符串回显到页面,否则它只是 PHP 中的一个字符串,您还缺少第一个字符串中的分号。

<?php 
    $images[0] = "image1";
    $images[1] = "image2";
    foreach ($images as $image) {
        if (count($images) > 1) {
            echo '<img src="'.$image.'" class="slide">
            <a href="#" class="zoom"><i class="icon-popup"></i></a>';
        } else {
            echo '<img class="slide" src="no-image-large.jpg">';
        }
    } 
?>