Home  >  Article  >  Backend Development  >  How to use php function recursively and the difference between return and echo_PHP tutorial

How to use php function recursively and the difference between return and echo_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:37:16962browse

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