How PHP function usage recursion and return and echo differences
PHP function usage how recursion and return and echo differ.
Code:
' Apple ',1=> ' banana ',2=> ' cat ',3=> ' dog ',4=> ' egg ', ' 5 ' = ' father '; Function Usage 1//arr is the incoming data $con is the conditional function f_1 ($arr, $con) {//the array here is private within this function, does not collide with the array that is coming in//So, the field array is not directly used inside the polygon, Inside the array can not be directly outside with//First instance an array $array = array (); The for-foreach while usage is similar to the specific Baidu foreach ($arr as $key + = $value) {//If the loop-out value equals con, add him to the array if ($value = = $ Con) {//arrays and variables differ in the addition of a [] $array [] = Array ($key + = $value);}//Script Academy http://www.jbxue.com}//loop to get the results back to the array. So, this function is an array return $array; When the return is executed, it breaks, no matter what code is not executed//return can be seen as a place where a function ends}//function usage 2//$con can be array function f_2 ($arr, $con) {//instance one first Variable $code = '
'; foreach ($arr as $key + $value) {//inside the For loop is the loop out con content foreach ($con as $value 2) {//. = add more contiguous definition variables later//if the first layer of data loops out Value, and the second layer condition loop appears the same value, added to the variable//multiple for loop to filter the data is also known as recursive if ($value = = $value 2) {$code. = '
- '. $value. '
'; }}} $code. = '
'; Returns the variable after the result of the loop. So, this function is a string return $code; }//function Usage 3//What is the difference between echo and return in a function see execution result function f_3 ($arr, $con) {//First instance a variable echo '
'; foreach ($arr as $key + $value) {//inside the For loop is the loop out con content foreach ($con as $value 2) {//. = add more contiguous defined variables later//IF The first layer of data loops out the value, and the second condition loop appears the same value, added to the variable//multiple for loop to filter the data is also known as recursive if ($value = = $value 2) {echo '
- '. $value. '
'; }}} ' echo '
'; }?> f_1 Output start
F_1 Output End
F_2 Output Start
Scripting Academy Http://www.jbxue.com
F_2 Output End
F_2 Output Start
F_2 Output End