存储函数导致变量

时间:2014-02-03 22:32:24

标签: php

这是我的代码

function result (){
    $args = array(
         'child_of' => 22,
         'hide_empty' => 0
     );
    $categories = get_categories($args);
      foreach($categories as $category) { 
        echo '<p><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
         } 
}

现在我需要将结果存储在变量中。它可能在php ??

2 个答案:

答案 0 :(得分:1)

    function result (){
        $args = array(
            'child_of' => 22,
            'hide_empty' => 0
        );
        $result='';
        $categories = get_categories($args);
        foreach($categories as $category) {
            $result.= '<p><a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
        }
        return $result;
    }

答案 1 :(得分:0)

对于任何函数foo,使用return语句

返回一个值
function foo(){
    return "bar";
}

$foo = foo();
echo $foo;//displays bar