递归函数:调用php函数本身 [英] Recursive function: Call php function itself

查看:167
本文介绍了递归函数:调用php函数本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个函数可以调用它自己,如果它没有问题,你需要得到你的批准。

我只是想确保我做对了,这不会造成任何困惑。或者不这样做?

 <?php 

函数判断($ the_array){
foreach($ the_array as $ key => $ value){
switch($ key){
case'in':
echo $ value;
休息;

case'out':
echo $ value;
休息;

case'level':
echo'< ul>';
确定($ value);
echo'< / ul>';
休息;




code


$ b <

这是数组:

  $ the_array = array('=>'中的
'< li> ;简单IN< / li>',
'out'=>'< li>简单OUT< / li>',
'level'=> array(
'in '>'< li>简单IN 2< / li>',
'out'=>'< li>简单OUT 2< / li>',
'level'= >数组(
'in'=>'< li>简单IN 3< / li>',
'out'=>'< li>简单OUT 3< / li> '
),
),
);

以下是最终的init:

  echo'< ul>'; 
确定($ the_array);
echo'< / ul>';

结果就是我想成为的样子,效果很好,但我不知道这是一个很好的做法。

解决方案

递归函数是可以的 - 但如果您不确定自己在做什么, 。如果函数有可能在递归循环中结束(它会一直重复调用自己),您将会超时,耗尽内存或造成僵尸启示。



把递归电话想象成一把非常非常锋利的小刀 - 在经验丰富的厨师的手中,这是在天堂里做的一个匹配,在洗碗机的手中,这是一个丢失的手指,等待发生。 / p>

PHP会尝试打好,并且默认情况下将递归深度限制为100(尽管可以更改),但对于几乎所有情况,如果递归深度达到100,事故已经发生,PHP通过阻止任何额外的行人流入交通而作出反应。 :)

I just want to make sure I do it right and this will not create any conficts.

I have a function which calls itself and need your approval if it's OK or not to do so?

<?php

function determine($the_array){
    foreach ($the_array as $key => $value) {
        switch ($key) {
            case 'in':
                    echo $value;
                break;

            case 'out':
                    echo $value;
                break;

            case 'level':
                    echo '<ul>';
                    determine($value);
                    echo '</ul>';
                break;

        }
    }

}

This is the array:

$the_array = array(
    'in' => '<li>Simple IN</li>',
    'out' => '<li>Simple OUT</li>',
    'level' => array(
            'in' => '<li>Simple IN 2</li>',
            'out' => '<li>Simple OUT 2</li>',
            'level' => array(
                'in' => '<li>Simple IN 3</li>',
                'out' => '<li>Simple OUT 3</li>'
            ),
        ),
);

And here is the final init:

echo '<ul>';
determine($the_array);
echo '</ul>';

The result is just how I wanted to be, it works great, but I don't know if this is a good practice.

解决方案

Recursive functions are OK - but dangerous if you aren't sure you know what you are doing. If there is any chance that a function will end up in a recursive loop (where it keeps calling itself over and over) you will either time out, run out of memory or cause a zombie apocalypse.

Think of recursive calls as a really, really sharp knife - in the hands of an experienced chef, it's a match made in heaven, in the hands of the dishwasher, it is a lost finger waiting to happen.

PHP tries to play nice, and limits a recursive depth to 100 by default (though this can be changed) but for almost all cases, if your recursive depth gets to 100, the accident has already happened and PHP reacts by stopping any additional pedestrians from wandering into traffic. :)

这篇关于递归函数:调用php函数本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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