PHP递归函数返回null而变量有值 [英] Php recursive function return null while variable have value

查看:21
本文介绍了PHP递归函数返回null而变量有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此函数返回NULL,而$alias 在第二次递归中具有值.在第一次调用时,它返回所需的值,但是当第一次不匹配时,它首先在 $alias 变量中的 avilable 中递归所需的值,但它不返回任何内容.

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);
        } 
    }

推荐答案

你忘记返回递归调用的结果:

You forgot to return the result of the recursion call:

return $this->checkAlias($result_string);

这篇关于PHP递归函数返回null而变量有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆