html作为php变量未正确呈现

Having the following method

  public function printDropdownTree($tree, $r = 0, $p = null) {
        foreach ($tree as $i => $t) {
            $dash = ($t['parent'] == 0) ? '' : str_repeat('-', $r) . ' ';
            printf("\t<option value='%d'>%s%s</option>
", $t['id'], $dash, $t['name']);
            if ($t['parent'] == $p) {
                // reset $r
                $r = 0;
            }
            if (isset($t['_children'])) {
                $this->printDropdownTree($t['_children'], ++$r, $t['parent']);
            }
        }
    }

which prints out nested select options and which works fine. But I would like to return the result as variable and I was trying to achieve this like

  public function printDropdownTree($tree, $r = 0, $p = null) {
        $html = "";
        foreach ($tree as $i => $t) {
            $dash = ($t['parent'] == 0) ? '' : str_repeat('-', $r) . ' ';
            $html .= '<option value="'.$t['id'].'">'.$dash.''.$t['name'].'</option>';
            if ($t['parent'] == $p) {
                // reset $r
                $r = 0;
            }
            if (isset($t['_children'])) {
                $this->printDropdownTree($t['_children'], ++$r, $t['parent']);
            }
        }


         return $html;
    }

but the $dash won't get rendered

The simple solution: You have to fetch the result of the recursive calls of the function!

public function printDropdownTree($tree, $r = 0, $p = null) {
    $html = "";
    foreach ($tree as $i => $t) {
        $dash = ($t['parent'] == 0) ? '' : str_repeat('-', $r) . ' ';
        $html .= '<option value="'.$t['id'].'">'.$dash.''.$t['name'].'</option>';
        if ($t['parent'] == $p) {
            // reset $r
            $r = 0;
        }
        if (isset($t['_children'])) {
            $html .= $this->printDropdownTree($t['_children'], ++$r, $t['parent']);
        }
    }


     return $html;
}