How to use phpfunction recursion and difference between return and echo

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

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.