mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2026-11-25 06:57:02 +08:00
* refactor: clean to roadmap content * refactor: move shared helpers into scripts/lib
18 lines
905 B
Markdown
18 lines
905 B
Markdown
# Recursion
|
|
|
|
Recursion, as it applies to PHP, refers to a function that calls itself to solve a problem. A recursive function distinguishes itself by solving small parts of the problem until it resolves the main issue. Think of it as breaking down a task into smaller tasks that are easier to solve. However, careful design is needed to ensure the recursive function has a clear stopping point, or else it can result in an infinite loop. Here's a quick example of a simple recursive function in PHP:
|
|
|
|
function countDown($count) {
|
|
echo $count;
|
|
if($count > 0) {
|
|
countDown($count - 1);
|
|
}
|
|
}
|
|
countDown(5);
|
|
|
|
|
|
In this example, the function `countDown` calls itself until the count hits zero, displaying numbers from 5 to 0.
|
|
|
|
Visit the following resources to learn more:
|
|
|
|
- [@official@Functions - Recursion](https://www.php.net/manual/en/language.functions.php) |