This article mainly introduces how to use phpfunction recursion and the difference between return and echo. For more information, see
This article mainly introduces how to use php function recursion and the difference between return and echo. For more information, see
The Code is as follows:
// Simulate SQL data
$ Array = array (0 => 'apple', 1 => 'bana', 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
// Therefore, the array in the field is not used directly in the internal area, and the array in the external area cannot be used directly.
// First instance an array
$ Array = array ();
// For foreach while usage is similar, specific baidu
Foreach ($ arr as $ key => $ value ){
// If the cyclic value is equal to con, add it to the array.
If ($ value = $ con ){
// The difference between an array and a variable is that a [] is added.
$ Array [] = array ($ key => $ value );
}
}
// Returns an array after the result is obtained in a loop. Therefore, this function is an array.
Return $ array;
// After the return statement is executed, it will be disconnected and no code will be executed.
// Return can be seen as the end of a function.
}
// Function usage 2
// $ Con can be an array
Function f_2 ($ arr, $ con ){
// Instance a variable first
$ Code ='
';
Foreach ($ arr as $ key => $ value ){
// The for loop in It loops through con content
Foreach ($ con as $ value2 ){
//. = Add more consecutive variable definitions later
// If the value generated by the first data loop is the same as that generated by the second conditional loop, add it to the variable.
// Multiple for loops are used to filter data, also known as recursion.
If ($ value = $ value2 ){
$ Code. ='
- '. $ Value .'
';
}
}
}
$ Code. ='
';
// Return the variable after the result is obtained in a loop. Therefore, this function is a string.
Return $ code;
}
// Function usage 3
// The difference between echo and return in the function depends on the execution result.
Function f_3 ($ arr, $ con ){
// Instance a variable first
Echo'
';
Foreach ($ arr as $ key => $ value ){
// The for loop in It loops through con content
Foreach ($ con as $ value2 ){
//. = Add more consecutive variable definitions later
// If the value generated by the first data loop is the same as that generated by the second conditional loop, add it 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 has already been echo in the function, so no echo is required during function execution.
$ Con = array ('apple', 'Father ');
F_3 ($ array, $ con );
?>
F_2 output end