PHP function usage how recursion and return and Echo differ

Source: Internet
Author: User
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
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.