使用if与我的foreach结构

时间:2013-01-25 08:03:46

标签: php if-statement foreach

我为foreach使用以下结构如何在同一格式中使用if语句?

<?php foreach($property as $section):?>


    if(!empty($section))
    {
      echo <p>data here </p>;
    }else{
      html
    }

<?php endforeach;?>

6 个答案:

答案 0 :(得分:3)

<?php foreach($property as $section):
    if(!empty($section)):
      echo "<p>data here </p>";
    endif;
endforeach;?>

OR

<?php foreach($property as $section):
    if(!empty($section)):
      echo "<p>data here </p>";
    else:
       echo "html";
    endif;
endforeach;?>

请参阅:Alternative Syntax

答案 1 :(得分:1)

不确定你的意思,但你没有有效的PHP。

foreach($property as $section) {
    if(!empty($section))
    {
      echo '<p>data here </p>';
    }else{
      echo 'html';
    }
}

答案 2 :(得分:0)

我认为你正在寻找这个。

<?php foreach($property as $section): ?>
    <?php
        if(!empty($section)) :
            echo <p>data here </p>;
        else :
            html
        endif;
endforeach; ?>

答案 3 :(得分:0)

SHORTHAND:

<?php foreach($property as $section):?>
    <?php if(!empty($section)):?>
      <p>data here </p>;
    <?php else: ?>
      html
    <?php endif;?>
<?php endforeach;?>

OTHER:

<?php foreach($property as $section)
 {
   if(!empty($section))
   {
     echo '<p>data here </p>';
   }else{
       echo 'html';
   }
 }
?>

答案 4 :(得分:0)

这是一个包含}elseif{的示例:

<?php foreach($property as $section):?>

<?php    if(!empty($section)): ?>

      <p>data here </p>

<?php   elseif ([another condition]): ?>

      ...

<?php   else: ?>

      html

<?php   endif: ?>

<?php endforeach;?>

整个文档位于:http://php.net/manual/en/control-structures.alternative-syntax.php

答案 5 :(得分:0)

我认为最佳解决方案是:

<?php
foreach($property as $section) {
    if(!empty($section))
    {
        echo '<p>data here </p>';
    }
    else
    {
        ?>
        <div id="dosome">really crazy html stuff here</div>
        <?php
    }
}
?>