doujiao1180
doujiao1180
2016-09-12 10:42

为什么我的函数返回值不可见?

已采纳

im dealing with encapsulated categories here. I want to resolve them until i reached the top layer by recursive function:

    function resolve_kat($kat_id){

    ...credentials here...
    $db = new mysqli($host_name, $user_name, $password, $database, NULL, $socket);

    $sql_kat = "SELECT * FROM categories WHERE category=".$kat_id;
    $kat_res = $db->query($sql_kat);
    $kat = $kat_res->fetch_assoc();

    if($kat['upppercategory']==0){
        return $kat['category'];
    }
    else {
        resolve_kat($kat['upppercategory']);
    }

    $kat_res->close(); 
}

$passtome = resolve_kat(927);

echo $passtome;

It works fine with echo, but i want to pass the value to an other function. What did i wrong?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongzhan8001 dongzhan8001 5年前

    You look to be mising a return in your IF. Should be:

    if($kat['upppercategory']==0){
       return $kat['category'];
    }
    else {
       return resolve_kat($kat['upppercategory']);
    }
    

    You can then pass it to another function.

    $passtome = resolve_kat(927);
    
    function otherFunction ($passtome) {
      #What you want to do...
    }
    
    点赞 评论 复制链接分享