Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
675 views
in Technique[技术] by (71.8m points)

recursion - Php recursive function return null while variable have value

This function returns NULL while $alias having value in second recursion. In first call it return the required value but when first if not matched and it recurse first the required value in avilable in $alias variable but it does not return anything.

public function checkAlias($fname='',$lname=''){

        if(!empty($fname)){
        $fname = mysql_real_escape_string($fname);
        }
        if(!empty($lname)){
        $lname = mysql_real_escape_string($lname);
        }

    $alias = strtolower($fname).strtolower($lname);
    $sql = "Select ALIAS from table where ALIAS = '$alias'";
    $query = mysql_query($sql);
    $row = mysql_fetch_row($query);
    $string_length = strlen($alias) - 1;
    $result_string = substr($alias,0,$string_length);

    if(!$row){
            print $alias;   // is printing value 
        return $alias;  // but here it returns null
    }else{
        $this->checkAlias($result_string);
        } 
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You forgot to return the result of the recursion call:

return $this->checkAlias($result_string);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...