How is php used recursively and the difference between return and echo

  • 2021-01-25 07:13:24
  • OfStack

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

//function  usage 1 
//arr  It's the incoming data  $con  Is the condition  
function f_1($arr,$con){ 
// Here,  array  It's private in this function, it's not going to come out array conflict  
// So, out of the way  array Use it on the inside, inside array You can't just use it outside  
// First instance 1 a array 
$array = array(); 
//for foreach while  Usage is similar, specific baidu 
foreach ($arr as $key => $value) { 
// If the loop comes out  value  Is equal to the  con  If so, add it to the array  
if ($value == $con) { 
// The difference between an array and a variable is that we add a  [] 
$array[] = array($key => $value); 
} 
} 
// After the loop gets the result   Returns an array. So, this function is going to be 1 An array  
return $array; 
//return  And when it's executed, it's going to break, no matter what else comes after it, right   Will not be enforced  
//return You can think of it as 1 Where the function ends  
} 


//function  usage 2 
//$con  It could be an array  
function f_2($arr,$con){ 
// First instance 1 A variable  
$code = '<ul>'; 
foreach ($arr as $key => $value) { 
// The inside of the for cycle   is   Circle the con content  
foreach ($con as $value2) { 
// .=  Add more in the future   Continuously defined variable  
//  If the first 1 Layer data loops out the values, and the 2 Layer condition loops appear with the same values that are added to   variable  
// multiple for Loop to filter data is also called   recursive  
if ($value == $value2) { 
$code .= '<li>'.$value.'</li>'; 
} 
} 
} 
$code .= '</ul>'; 
// After the loop gets the result   Returns a variable. So, this function is going to be 1 A string  
return $code; 
} 

//function  usage 3 
// In the function  echo  and  return  What's the difference   Look at the execution result  
function f_3($arr,$con){ 
// First instance 1 A variable  
echo '<ul>'; 
foreach ($arr as $key => $value) { 
// The inside of the for cycle   is   Circle the con content  
foreach ($con as $value2) { 
// .=  Add more in the future   Continuously defined variable  
//  If the first 1 Layer data loops out the values, and the 2 Layer condition loops appear with the same values that are added to   variable  
// multiple for cycle   De-filtering data is also known as   recursive  
if ($value == $value2) { 
echo '<li>'.$value.'</li>'; 
} 
} 
} 
echo '</ul>'; 
} 
?> 

f_1 output start<br/> 
<?php 
// because  f_1  is 1 We can print it out  
print_r(f_1($array,'banana')); 
?> 
<br/>f_1 output end 
<hr/><br/> 
f_2 output start<br/> 
<?php 
//f_2  Is a 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  It's already in the function echo  So it's not used when the function is executed echo 
$con = array('apple','father'); 
f_3($array,$con); 
?> 
<br/>f_2 output end 

Related articles: