php递归函数实用吗,用外行人的话说,什么是使用PHP的递归函数

递归是重复自身的东西。就像一个从内部调用自己的函数。让我在一个有点伪的例子中演示一下:

想象一下你和你的朋友们一起喝啤酒,但如果你午夜之前不回家,你的妻子会给你下地狱的。为此,让我们创建orderAndDrinkBeer($time)函数,其中$time是午夜,减去您完成当前饮料和回家所需的时间。

所以,到了酒吧,你点了第一瓶啤酒,开始喝酒:$timeToGoHome = '23';  // Let's give ourselves an hour for last call and getting homefunction orderAndDrinkBeer($timeToGoHome) {  // Let's create the function that's going to call itself.

$beer = New Beer();  // Let's grab ourselves a new beer

$currentTime = date('G'); // Current hour in 24-hour format

while ($beer->status != 'empty') {  // Time to commence the drinking loop

$beer->drink();  // Take a sip or two of the beer(or chug if that's your preference)

}

// Now we're out of the drinking loop and ready for a new beer

if ($currentTime 

orderAndDrinkBeer($timeToGoHome);  // So we make the function call itself again!

} else {  // Aw, snap!  It is time :S

break; // Let's go home :(

}}

现在,我们只希望你不能喝足够的啤酒,以至于你的妻子会让你睡在沙发上,不管你是否准时回家

但是的,递归就是这样的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值