Home >Backend Development >PHP Problem >What is the difference between recursion and iteration in php
The difference between recursion and iteration in php is that recursion means that the program calls itself, while iteration means using the original value of the variable to calculate a new value of the variable, which means that A keeps calling B.
The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.
Recursion refers to the programming technique of calling the program itself, which is called recursion
Advantages:
1) Turning big problems into small ones can greatly reduce the amount of code;
2) Use limited statements to define an infinite collection of objects;
3) The code is more concise, clear and more readable
Disadvantages:
1) Calling functions recursively is a waste of space;
2) Recursion that is too deep can easily cause stack overflow;
Iteration refers to using the original value of a variable to calculate a new value of the variable. Iteration is A keeps calling B.
Advantages:
1) High iteration efficiency, the running time only increases due to the increase in the number of loops;
2)No additional overhead, space There is nothing added,
Disadvantages:
1) Not easy to understand;
2) The code is not as concise as recursion;
3) Writing complex problems times are difficult.
The relationship between the two:
1) There must be iteration in recursion, but there may not be recursion in iteration, and most of them can be converted into each other.
2) If you can use iteration, do not use recursion. Calling functions recursively is a waste of space, and recursion that is too deep can easily cause stack overflow. /*relative*/
Related recommendations: php Video tutorial
The above is the detailed content of What is the difference between recursion and iteration in php. For more information, please follow other related articles on the PHP Chinese website!