What is the essential difference between recursion and loop

  ios, question
public function noLimitCategory($categories,$top_id=0,$level=0){
 static $arr=array();
 //Traverse array
 foreach($categories as $category){
 //Number of categories at current level
 $category['level']=$level;
 if($category['parent_id']==$top_id){
 $arr[]=$category;
 $ this-> nolimitcategory ($ categories, $ category ['id'], $ level plus 1);  //Recursion
 bracket
 bracket
 //echo '<pre>';
 //var_dump($categories);  exit;
 return $arr;
 bracket

The most essential difference between recursion and circulation is What? For example, in the above recursion, does each recursion create a new scope, that is to say, the $level plus 1 here is actually 0 plus 1 each time, right

Functionally speaking, all those implemented by recursion can be implemented by loops, but sometimes recursion is more convenient. From the efficiency point of view, loops are generally larger than recursion.

As the landlord said, every recursion is to create a new scope, andLevelIn fact, in different scopes, eachLevelAre all of the scope of the upper levelLevel plus 1Every timeLevelIt’s all different, so it’s not0 plus 1, theseLevelThey are different variables, although they are just different variables with the same name.
Write a circular version for you, no optimization,LevelCan not be an array, in order to make you clear each recursiveLevelNot the same oneLevelAnd wrote

public function noLimitCategory($categories,$top_id=0,$level=0){
 static $arr=array();  //This static is not required because it is recursive
 $top_id = array();
 $level = array();
 
 $top_id[0] = 0;
 $level[0] = 0;
 $i = 0;
 do{
 foreach($categories as $category){
 $category['level']=$level[$i];
 if($category['parent_id']==$top_id[$i]){
 $arr[]=$category;
 $top_id[] = category['id'];
 $level[] = $level[$i] plus 1;
 bracket
 bracket
 $ igaga;
 }while($i<count(top_id))
 //echo '<pre>';
 //var_dump($categories);  exit;
 return $arr;
 bracket

Visible, if written as a cycle, need to save or distinguish themselvestop_idAndLevelAnd recursion is in different scopes, so implication means differenttop_idAndLevel.

When we say recursion efficiency is low, we don’t just need to create one more function call each time.top_idAndLevel(The above loop is just creating a few moretop_idAndLevel), and there should be other places to save the return address of each linear function call and copy it once.categoriesA reference to. Besides, as I said, in the cycleLevelCan not be used as an array, so it just needs to be allocated and saved more.top_idThe space. Therefore, the cycle efficiency is higher than recursion.

Recursion essentially uses the stack provided by the compiler to implement this algorithm, so if you write a loop that requires you to maintain the structure of this stack, and on this basis optimize everything you don’t need (what you can definitely optimize is the return address of the function), you will get an equivalent loop version. In addition, not all programs use stack with the highest efficiency, but this writing loop can use other data structures to achieve the purpose of optimization. The problem is that loops need to maintain their own stack (which can be optimized in some cases), so the code is complicated.

For ordinary people, once they have learned recursion, they will encounter a slightly more complicated greedy algorithm, dynamic programming, traversal and other problems, then recursion is the most easily thought of. From this perspective, recursive routine is simpler.