您能帮助我为什么递归功能不起作用吗?如果第一个参数匹配,则函数返回正确的值。如果不是,则返回值为空。
function stockToDate($base_date, $i) {
include "../lib/dbconn.php" ;
$today = date("Y-m-d") ;
$base_date = date("Y-m-d", strtotime($base_date . "-1day")) ;
$q = " SELECT txn_date "
. " FROM tworking_ymd "
. " WHERE txn_date = '$base_date' " ;
$r = mysqli_query($dbc, $q) ;
if(mysqli_num_rows($r) == 1){
$row = mysqli_fetch_row($r);
return $row[0] ;
} else {
$i = $i + 1 ;
if($i > 10) {
return $today ;
} else {
stockToDate($base_date, $i) ;
}
}
}
答案 0 :(得分:0)
函数每次调用自身时,都需要能够将返回值传递给调用堆栈。您错过了返回
function stockToDate($base_date, $i) {
include "../lib/dbconn.php" ;
$today = date("Y-m-d") ;
$base_date = date("Y-m-d", strtotime($base_date . "-1day")) ;
$q = " SELECT txn_date "
. " FROM tworking_ymd "
. " WHERE txn_date = '$base_date' " ;
$r = mysqli_query($dbc, $q) ;
if(mysqli_num_rows($r) == 1){
$row = mysqli_fetch_row($r);
return $row[0] ;
} else {
$i = $i + 1 ;
if($i > 10) {
return $today ;
} else {
return stockToDate($base_date, $i) ;
}
}
}