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!

php函数返回值只能有一个。在PHP中,函数返回值使用return语句定义,语法“return 返回值;”。return语句只能返回一个参数,即函数只能有一个返回值;如果要返回多个值的话,就需在函数中定义一个数组,将返回值存储在数组中返回。

不是,php传参可以是字符串、数字、布尔值、数组等。从PHP5.6版本开始支持传递数组参数,函数的形式参数可使用“…”来表示函数可接受一个可变数量的参数,而可变参数将会被当作一个数组传递给函数,语法“function 函数名(...$arr){//执行代码}”。

php函数的参数赋值有3种:1、值传递赋值,将实参的值复制一份再赋值给函数的形参;2、引用传递赋值,把实参的内存地址复制一份,然后传递给函数的形参,进而将实参值赋值给形参;3、直接给函数的参数指定默认值,语法“函数名(参数变量='值')”。

PHP作为一种非常流行的脚本语言,有着强大的函数库支持,其函数的命名规范和规则对于开发效率和代码可读性都有着重要的影响。本文将介绍PHP函数的命名规范及规则。一、命名风格在PHP中,函数名需要严格符合命名规范和规则,规范主要包括两个方面:命名风格和命名规则。1.下划线命名法下划线命名法是PHP函数命名最常用的方式,也是官方推荐的一种方式。遵循这种方式的函数名

随着互联网技术的发展,PHP已经成为了非常流行的开发语言之一。身为一个PHP开发者,了解PHP函数和方法的区别是非常重要的,因为它们在编写代码的时候都是必不可少的。在本文中,我们将详细介绍PHP函数和方法的区别。

随着现代编程语言的不断发展,编程的效率和功能性也不断提高,其中PHP作为一种广泛使用的服务器端脚本语言,也在不断地更新和完善其自身的功能列表。PHP函数的迭代器函数就是其中的一种新功能,为PHP程序员提供了更加灵活和高效的编程方式。在本文中,我们将详细介绍PHP函数的迭代器函数的相关知识。什么是PHP函数的迭代器函数?在介绍PHP函数的迭代器函数之前,我们首

在php中,递归函数指的是自调用函数,也就是函数在函数体内部直接或间接地自己调用自己;使用递归函数时,需要在函数体中附加一个判断条件,以判断是否需要继续执行递归调用,当条件满足时会终止函数的递归调用。

PHP是一种流行的服务器端编程语言,它拥有许多功能强大的函数和方法。在PHP函数中,true表示什么意思,这是一个非常基础的问题,但它却是非常重要的。在本文中,我们将详细探讨php函数中true的意思及其使用方法。


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

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft