php附加在foreach中,有什么用于追加变量。

时间:2013-01-31 14:12:51

标签: php

如何将代码附加到变量。

<?php
echo "<ul>\n";
    foreach($related as $r) {

        if ($r->Count > 0) {
            echo "<li>" . "<a>". $r->post_title ."</a>". "\n";
      f_tree_av($r->post_id, $level + 1);
      echo "</li>";
        } elseif ($r->Count==0) {
            echo "<li>" . "<a>". $r->post_title ."</a>". "\n";
        } else;
    }
    echo "</ul>\n";
?>

2 个答案:

答案 0 :(得分:1)

<?php
$content .= "<ul>\n";
    foreach($related as $r) {

        if ($r->Count > 0) {
            $content .= "<li>" . "<a>". $r->post_title ."</a>". "\n";
      f_tree_av($r->post_id, $level + 1);
      echo "</li>";
        } elseif ($r->Count==0) {
            $content .= "<li>" . "<a>". $r->post_title ."</a>". "\n";
        } else;
    }
   $content .= "</ul>\n";
?>

答案 1 :(得分:-1)

只需使用.=运算符,即$a = $a . "...";$a .= "..."

<?php
$content = "<ul>\n";
    foreach($related as $r) {

        if ($r->Count > 0) {
            $content .= "<li>" . "<a>". $r->post_title ."</a>". "\n";
      f_tree_av($r->post_id, $level + 1);
                  $content .= "</li>";
        } elseif ($r->Count==0) {
                        $content .= "<li>" . "<a>". $r->post_title ."</a>". "\n";
        } else;
    }
    echo $content . "</ul>\n";
?>

Documentation