Home  >  Article  >  Backend Development  >  在下面的具体代码中,如何去掉引用参数,改用返回值?

在下面的具体代码中,如何去掉引用参数,改用返回值?

WBOY
WBOYOriginal
2016-07-06 13:52:05985browse

代码如下:

<code>//返回指定的$aid 的工程类别id和他的下级类别id
//参数:
//  $aid 工程类别, int类型
//  $conn 数据库连接
//  $str,返回值,字符串类型
// 返回: 字符串,类似于 "1,3,7,8,"

function get_gongchengleibie($aid, &$str, $conn){
    $str = "";
    $query=mysql_query(" select * from gongchengleibie where pid=$aid",$conn);
    while($row=mysql_fetch_array($query))
    {
        $str .= $row["id"].",";
        get_gongchengleibie($row["id"], $str1, $conn);
    }
} </code>

gongchengleibie为数据库表,3层树结构,如下图
在下面的具体代码中,如何去掉引用参数,改用返回值?

我想去掉引用参数$str,改为用返回值的方式,自己尝试失败了。
请问该怎么改?
如果不能改,是什么原因造成的?

感谢各位大神的关注或回答。
如有信息不全或问题描述不清,sorry + 请求告知,我会立即补充。

回复内容:

代码如下:

<code>//返回指定的$aid 的工程类别id和他的下级类别id
//参数:
//  $aid 工程类别, int类型
//  $conn 数据库连接
//  $str,返回值,字符串类型
// 返回: 字符串,类似于 "1,3,7,8,"

function get_gongchengleibie($aid, &$str, $conn){
    $str = "";
    $query=mysql_query(" select * from gongchengleibie where pid=$aid",$conn);
    while($row=mysql_fetch_array($query))
    {
        $str .= $row["id"].",";
        get_gongchengleibie($row["id"], $str1, $conn);
    }
} </code>

gongchengleibie为数据库表,3层树结构,如下图
在下面的具体代码中,如何去掉引用参数,改用返回值?

我想去掉引用参数$str,改为用返回值的方式,自己尝试失败了。
请问该怎么改?
如果不能改,是什么原因造成的?

感谢各位大神的关注或回答。
如有信息不全或问题描述不清,sorry + 请求告知,我会立即补充。

改成这样就行了,多的就不说了。

<code>function get_gongchengleibie($aid, $conn){
    $str = "";
    $query=mysql_query(" select * from gongchengleibie where pid=$aid",$conn);
    while($row=mysql_fetch_array($query))
    {
        $str .= $row["id"].",";
        $str .= get_gongchengleibie($row["id"], $conn);
    }
    return $str;
} </code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn