具体代码如下:<?php
$proImgs = getAllImgByProId($row['id']);
//在这个位置希望加入 if ($proImgs != null)  判断proImgs是否为空,不为空才进行foreach
foreach ($proImgs as $img):
?>
<p>html</p>
<?php endforeach;?>
但在注释位置添加 if无法像普通if  else进行判断,会提示error,我该如何进行加入判断   

解决方案 »

  1.   

    if ($proImgs != null) : 
    foreach ($proImgs as $img):
    ?>
    <p>html</p>
    <?php
    endforeach;
    endif;
    ?>
    其实并不需要判断 proImgs 是否为空,为空是不会进入 foreach 循环体的
      

  2.   

    正常写也是可以的,有头有尾就行,或者html用echo输出<?php
    if ($proImgs != null) {
    foreach ($proImgs as $img){
    ?>
    <p>html</p>
    <?php
    }
    }
    ?>或者<?php
    if ($proImgs != null) {
    foreach ($proImgs as $img){
    echo '<p>html</p>';
    }
    }
    ?>
      

  3.   

    $proImgs != null
    还是
    $proImgs != NULL
      

  4.   

    你这种方法不行啊,会显示语法错误:Parse error: syntax error, unexpected T_ENDFOREACH
      

  5.   

    版主那个还要加个<?php
      

  6.   

    直接用empty判断更好些。