search
HomeBackend DevelopmentPHP TutorialHow to use php function recursively and the difference between return and echo_PHP tutorial

Copy code The code is as follows:

//Simulate sql data
$array = array(0 =>'apple',1=>'banana',2=>'cat',3=>'dog',4=>'egg','5'=>'father');

//function usage 1
//arr is the incoming data $con is the condition
function f_1($arr,$con){
//array here is this function It is private inside and will not conflict with the outside array
//So, the outside array cannot be used directly inside, and the inside array cannot be used directly outside
//First instance an array
$ array = array();
//for foreach while usage is similar, specifically baidu
foreach ($arr as $key => $value) {
//If the value looped out is equal to con , add it to the array
if ($value == $con) {
//The difference between arrays and variables is the addition of []
$array[] = array($key => ; $value);
}
}
//Return the array after looping to get the result. Therefore, this function is an array
return $array;
//return will be terminated after execution, no matter what code follows it will not be executed
//return can be regarded as a function Ending place
}


//function usage 2
//$con can be an array
function f_2($arr,$con){
//First Instance a variable
$code = '
    ';
    foreach ($arr as $key => $value) {
    //The for loop inside is to loop out the con content
    foreach ($con as $value2) {
    // .= Add more continuous definition variables in the future
    // If the value of the first layer of data loop is the same as the value of the second layer of conditional loop, Add to variables
    //Multiple for loops to filter data are also called recursion
    if ($value == $value2) {
    $code .= '
  • '.$value. '
  • ';
    }
    }
    }
    $code .= '
';
//Return the variable after looping to get the result. So, this function is a string
return $code;
}

//function usage 3
//What is the difference between echo and return in the function? See the execution result
function f_3($arr,$con){
//First instance a variable
echo '
    ';
    foreach ($arr as $key => $value) {
    //The for loop inside is to loop out the con content
    foreach ($con as $value2) {
    // .= Add more continuously defined variables in the future
    // If the first layer of data is looped out The value is the same as the value appearing in the second level conditional loop, and is added to the variable
    //Multiple for loops to filter data are also called recursion
    if ($value == $value2) {
    echo '
  • '.$value.'
  • ';
    }
    }
    }
    echo '
';
}
?>

f_1 output start

//Because f_1 is an array, we can print it out
print_r(f_1($array,' banana'));
?>

f_1 output end



f_2 output start

//f_2 is a variable
$con = array('apple','father');
echo f_2($array,$con);
?>

f_2 output end



f_2 output start

// f_3 is already echoed in the function, so there is no need to echo when the function is executed
$con = array('apple','father');
f_3($array,$con);
?>

f_2 output end

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/736784.htmlTechArticleCopy the code as follows: ?php //Simulate sql data $array = array(0='apple',1 ='banana',2='cat',3='dog',4='egg','5'='father'); //function usage 1 //arr is the incoming data $con is the condition. ..
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
C++ lambda 表达式是否支持递归?C++ lambda 表达式是否支持递归?Apr 17, 2024 pm 09:06 PM

是的,C++Lambda表达式可以通过使用std::function支持递归:使用std::function捕获Lambda表达式的引用。通过捕获的引用,Lambda表达式可以递归调用自身。

在Java中递归地计算子字符串出现的次数在Java中递归地计算子字符串出现的次数Sep 17, 2023 pm 07:49 PM

给定两个字符串str_1和str_2。目标是使用递归过程计算字符串str1中子字符串str2的出现次数。递归函数是在其定义中调用自身的函数。如果str1是"Iknowthatyouknowthatiknow",str2是"know"出现次数为-3让我们通过示例来理解。例如输入str1="TPisTPareTPamTP",str2="TP";输出Countofoccurrencesofasubstringrecursi

递归程序在C++中找到数组的最小和最大元素递归程序在C++中找到数组的最小和最大元素Aug 31, 2023 pm 07:37 PM

我们以整数数组Arr[]作为输入。目标是使用递归方法在数组中找到最大和最小的元素。由于我们使用递归,我们将遍历整个数组,直到达到长度=1,然后返回A[0],这形成了基本情况。否则,将当前元素与当前最小或最大值进行比较,并通过递归更新其值以供后续元素使用。让我们看看这个的各种输入输出场景−输入 −Arr={12,67,99,76,32};输出 −数组中的最大值:99解释 &mi

如何解决Python的最大递归深度错误?如何解决Python的最大递归深度错误?Jun 24, 2023 pm 02:48 PM

Python是一门易学易用的编程语言,然而在使用Python编写递归函数时,可能会遇到递归深度过大的错误,这时就需要解决这个问题。本文将为您介绍如何解决Python的最大递归深度错误。1.了解递归深度递归深度是指递归函数嵌套的层数。在Python默认情况下,递归深度的限制是1000,如果递归的层数超过这个限制,系统就会报错。这种报错通常称为“最大递归深度错误

如何使用Vue表单处理实现表单的递归嵌套如何使用Vue表单处理实现表单的递归嵌套Aug 11, 2023 pm 04:57 PM

如何使用Vue表单处理实现表单的递归嵌套引言:随着前端数据处理和表单处理的复杂性不断增加,我们需要通过一种灵活的方式来处理复杂的表单。Vue作为一种流行的JavaScript框架,为我们提供了许多强大的工具和特性来处理表单的递归嵌套。本文将向大家介绍如何使用Vue来处理这种复杂的表单,并附上代码示例。一、表单的递归嵌套在某些场景下,我们可能需要处理递归嵌套的

如何在Linux中使用递归“ls”如何在Linux中使用递归“ls”Mar 20, 2024 am 10:03 AM

在Linux系统中,“ls”命令是一个非常有用的工具,它提供了对当前目录中文件和文件夹的简洁概述。通过“ls”命令,您可以快速查看文件和文件夹的权限、属性等重要信息。虽然“ls”命令是一个基本的命令,但是通过结合不同的子命令和选项,它可以成为系统管理员和用户的重要工具。通过熟练使用“ls”命令及其各种选项,您可以更高效地管理文件系统,快速定位所需文件,以及执行各种操作。因此,“ls”命令不仅可以帮助您了解当前目录结构,还可以提高您的工作效率。比如,在Linux系统中,通过使用带有递归选项的"ls

Go语言中的循环和递归的比较研究Go语言中的循环和递归的比较研究Jun 01, 2023 am 09:23 AM

注:本文以Go语言的角度来比较研究循环和递归。在编写程序时,经常会遇到需要对一系列数据或操作进行重复处理的情况。为了实现这一点,我们需要使用循环或递归。循环和递归都是常用的处理方式,但在实际应用中,它们各有优缺点,因此在选择使用哪种方法时需要考虑实际情况。本文将对Go语言中的循环和递归进行比较研究。一、循环循环是一种重复执行某段代码的机制。Go语言中主要有三

利用ThinkPHP6实现递归树结构利用ThinkPHP6实现递归树结构Jun 20, 2023 pm 02:48 PM

随着互联网的发展,各种网站和应用程序中都出现了树形结构的展示,例如分类目录、人员组织架构、权限管理等。在这些应用场景中,递归树结构已经成为了非常重要且实用的模型之一。ThinkPHP6是一种基于MVC模型的PHP开发框架,其拥有丰富的扩展库和优秀的性能,广受开发者的认可和使用,而在ThinkPHP6中实现递归树结构也变得更加方便了。下面,我们将介绍如何在Th

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)