How to use phpfunction recursion and the difference between return and echo: how to use phpfunction recursion and the difference between return and echo. Code: & lt ;? Php // simulate SQL Data $ array = array (0 = & gt; 'apple', 1 = & gt; 'banana ', 2 = & gt; 'cat ', 3 = php function how to use recursion and difference between return and echo
How to use php function recursion and the difference between return and echo.
Code:
'Apple', 1 => 'banana ', 2 => 'cat', 3 => 'dog', 4 => 'egg ', '5' => 'Father '); // function usage 1 // arr is the input data $ con is the condition function f_1 ($ arr, $ con) {// The array here is private in this function and will not conflict with the forward array // so the array in the field is not directly used in the internal, the array in it cannot be used outside directly. // The first instance is an array $ array = array (); // The usage of foreach while is similar, specific baidu foreach ($ arr as $ key => $ value) {// if the cyclic value is con, add it to the array if ($ value = $ con) {// The difference between the array and the variable is that a [] $ array [] = array ($ key = > $ Value);} // script School http://www.jbxue.com} // returns an array after the result is returned in a loop. Therefore, this function is an array of return $ array; // it is disconnected after the return statement is executed, no matter what code will be executed, // return can be considered as the end of a function} // 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 consecutive defined variables later // if the value of the first layer of data loops is the same as the value of the second layer of condition loops, add to variable // multiple for loops to filter data, also known as recursive if ($ value = $ value2) {$ code. ='
- '. $ Value .'
';}}$ Code. ='
'; // The variable is returned after the result is obtained in a loop. Therefore, 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 consecutive defined variables later // if the value of the first layer of data loops is the same as the value of the second layer of condition loops, add to variable // multiple for loops to filter data is also known as recursive if ($ value = $ value2) {echo'
- '. $ Value .'
';}} Echo'
';}?> F_1 output start
F_1 output end
F_2 output start
// Script School http://www.jbxue.com
F_2 output end
F_2 output start
F_2 output end