What is the difference between recursion and iteration in php

王林
Release: 2023-03-11 09:48:01
Original
1567 people have browsed it

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.

What is the difference between recursion and iteration in php

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!