What does recursive function mean in php
In PHP, a recursive function refers to a self-calling function, that is, the function calls itself directly or indirectly within the function body; when using a recursive function, a judgment condition needs to be added to the function body to Determine whether the recursive call needs to continue. When the conditions are met, the recursive call of the function will be terminated.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Recursive functions are self-calling functions, that is, functions Call yourself directly or indirectly within the function body. It should be noted that when using a recursive function, a judgment condition is usually attached to the function body to determine whether the recursive call needs to continue. When the condition is met, the recursive call of the function will be terminated.
The biggest advantage of recursive functions is that they can simplify complex and repetitive procedures in the program, and can use this feature to perform some more complex operations. For example, operations such as lists, dynamic tree menus, and directory traversal. The corresponding non-recursive functions, although efficient, are difficult to program and relatively unreadable.
Recursive Thought
The main idea of recursion is to transform a relatively complex problem (original problem) into problems that are similar to the original problem. Solve smaller-scale problems (sub-problems). When each small problem is solved, the final big problem will naturally be solved.
The recursive method only needs a small amount of programs to describe the multiple repeated calculations required in the problem-solving process, greatly reducing the amount of program code. Of course, recursive functions are not perfect, and they also have certain shortcomings, that is, the recursive method functions do not operate efficiently.
The maximum number of recursion levels in PHP is not unlimited. This is related to the memory limit of the program. PHP5 allows a program to use 128M of memory by default, so when the number of recursion levels is too large, the 128M memory is exhausted. , the program will generate a fatal error and exit. PHP7 allows 256M of memory by default.
The maximum memory allowed by PHP can be modified by modifying the php.ini file as follows:
; Maximum amount of memory a script may consume (128MB) ; http://php.net/memory-limit memory_limit=256M
Recursion in PHP
To achieve recursion, the following two conditions need to be met:
The sub-problem must be the same thing as the original problem and simpler.
You cannot call yourself without limit, there must be an exit, and it can be simplified to non-recursive situation processing.
Example 1: Use a recursive function to implement the factorial of n!
<?php header('content-type:text/html;charset=utf-8'); function factorial($num){ //确定递归函数的出口 if($num == 1){ return 1; }else{ return $num*factorial($num - 1); } } echo '15 的阶乘是:'.factorial(15); ?>
The running results are as follows:
Example 2: Calculate the Fibonacci sequence.
The Fibonacci sequence is as follows:
1, 1, 2, 3, 5, 8, 13, 21, 34,...
The implementation code is as follows:
<?php header('content-type:text/html;charset=utf-8'); function demo($num){ // if($num == 1 || $num == 2){ return 1; }else{ return demo($num - 1) + demo($num - 2); } } echo '数列第 10 位是:'.demo(10); ?>
The running results are as follows:
PHP Video Tutorial 》
The above is the detailed content of What does recursive function mean in php. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Validator can be created by adding the following two lines in the controller.
