php递归

php递归求和

<?php

$num=3;

function sum($num){

      static $tot;

       if($num>=1){

              $tot+=$num;

               return sum(--$num);

        }else{

                return $tot;

        }

}

echo sum($num);

?>

<?php

function SontoFather($i)
{
        $database = pc_base::load_config('pcms_db');
        $db=new tg_mysql;
        $db->config($config=array(
        "host"=>$database["default"]["hostname"],
        "user"=>$database["default"]["username"],
        "pass"=>$database["default"]["password"],
        "database"=>$database["default"]["database"]));
        $sql = "SELECT parent_id,tag,tagid FROM `tags` WHERE 1 AND modelid=1 AND `tagid`=".$i;
        $result = $db->query($sql);
        $row=$db->fetch_array($result);
        if($row[0]!=0)
        {    
                #echo "yes"; 

                $i = $row[0];
                return $this->SontoFather($i);

      }else{
                #echo $row[1];
                //$j=$row[1];
                #echo "no";
                return $row[2];
        }    
}
echo SontoFather(1);
?>
数字库结构
id name parent_id
1 test 0
2 test2 1
3 test3 2

 

    A+
发布日期:2021年07月16日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: