How PHP function usages are recursive and return and echo differences

Source: Internet
Author: User
Tags foreach

  This article mainly describes how PHP function usage recursion and return and echo differences, the need for friends can refer to the following

  Code as follows: <?php //Analog 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) {  //The array here is private in this function, and will not be in the presence of the array conflict  //So, the field of the array is not directly in the inside, the array can not be directly outside with  //First Instance of a array  $ Array = array (); //for a foreach while usage is similar, specific baidu  foreach ($arr as $key => $value) { //If the value of the loop equals con, add him to the array   if ($value = = $con) { //arrays and variables differ in that a []  is added $array [] = Array ($key => $value);  } } //loop Gets the result and returns the array. So, this function is an array   return $array; //return after execution, no matter what code behind it will not be executed  //return can be considered as a function of the end of the place  }      //function usage 2 //$con can be array   function f_2 ($arr, $con) { //First Instance a variable   $code = ' <u L> ';  foreach ($arr as $key => $value) { //Inside for loop is recycled out of con content   foreach ($con as $value 2) { // . = add more continuous definition variables in the future  If the value of the first layer of data is recycled, the same value as the second level of the conditional loop appears, add to the variable  //multiple for loop to filter the data also known as recursion   if ($value = = $value 2) {  $code. = ' <LI&G t; '. $value. ' </li> '; } } }  $code. = ' </ul> '; //loop Gets the result and returns the variable. So, this function is just a string   return $code; }   //function usage 3 //What's the difference between Echo and return in a function see execution results   FUNCTI On F_3 ($arr, $con) { //First Instance a variable   echo ' <ul> ';  foreach ($arr as $key => $value) { //Inside for loop is Loop out con content   foreach ($con as $value 2) { //. = add more consecutive definition variables  //If the first-tier data is recycled and the same value as the second-level conditional loop appears, add to the variable   Multiple for loops to filter data also known as recursion   if ($value = = $value 2) {  echo ' <li> '. $value. ' </li> '; } } }  echo ' </ul> '; } ?>    f_1 output start<br/& gt;  <?php //Because f_1 is an array, we can print it out   Print_r (f_1 ($array, ' banana ')); ?>  1 output end  <hr/><br/>  f_2 output start<br/>  <?php //f_2 is variable   $con = Array (' Apple ', ' father ');  Echo f_2 ($array, $con); ?>  <br/>f_2 Output end  <hr/><br/>  f_2 output start<br/>  <?php //f_3 is already in the function echo, Therefore, the function does not need to echo  $con = Array (' Apple ', ' father ');  f_3 ($array, $con); ?>  output end ,  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.