search
HomeBackend DevelopmentPHP ProblemPHP recursive function usage

PHP recursive function usage

Jun 06, 2020 am 10:08 AM
php

PHP recursive function usage

PHP recursive function usage

The recursive function is a self-calling function, which performs self-tuning directly or indirectly in the function body, but needs to be set to If the condition is met, the function itself will be called. If it is not met, the self-tuning of this function will be terminated, and then the control of the process will be returned to the upper layer function for execution.

Code example

<?php
 //声明一个函数,用于测试递归
 function test($n){
   echo $n." ";        //在函数开始输出参数的值
   if($n>0){                //判断参数是否大于0
     test($n-1);            //如果参数大于0则调用自己,并将参数减1后再次传入
   }else{                   //判断参数是不大于0
     echo "<-------->  ";
   }
   echo $n." ";
 }
 test(10);                   //调用test函数将整数10传给参数
?>

First think about it, what is the final output of this example?

Okay, let’s take a look at the output result of this function: How about

 10 9 8 7 6 5 4 3 2 1 0 <--> 0 1 2 3 4 5 6 7 8 9 10

, I wonder if this result is the same as expected?

Step explanation

The first step is to execute test(10), echo 10, and then because 10>0, execute test(9) , there is still echo 10 that has not had time to be executed. The second step is to execute test (9), echo 9, and then because 9>0, execute test (8). Similarly, there is still echo that has not had time to execute. 9

The third step is to execute test(8), echo 8, and then because 8>0, execute test(7). There is also echo 8 that has not had time to be executed.

Fourth step Step 1, execute test(7), echo 7, and then execute test(6) because 7>0, there is also echo 7

that has not been executed in time. Step 5, execute test(6), echo 6. Then because 6>0, test (5) is executed. There is also echo 6

#......

Step 10. , execute test(0), echo 0. At this time, the condition of 0>0 is not satisfied. The test() function is no longer executed, but echo "", and the subsequent echo 0

10 9 8 7 6 5 4 3 2 1 0 <--> 0 1 2 3 4 5 6 7 8 9 10
# is executed. ##Recommended tutorial: "

PHP Tutorial"



The above is the detailed content of PHP recursive function usage. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MantisBT

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.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software