PHP function usage how recursion and return and Echo distinguish _php tips

Source: Internet
Author: User
Copy Code code as follows:

<?php
Simulate SQL data
$array = Array (0=> ' apple ',1=> ' banana ',2=> ' cat ',3=> ' dog ',4=> ' egg ', ' 5 ' => ' father ');

Function Usage 1
ARR is the incoming data $con is the condition
function F_1 ($arr, $con) {
The array here is private within this function and will not conflict with the array
Therefore, the field of the array is not directly in the face, inside the array can not be used directly outside
First instance an array
$array = Array ();
For foreach while usages are similar, specific to the Baidu
foreach ($arr as $key => $value) {
If the value of the loop is equal to con, add him to the array
if ($value = = $con) {
The difference between an array and a variable is the addition of a []
$array [] = Array ($key => $value);
}
}
Returns the array after the loop gets the result. So, this function is an array
return $array;
Return execution will be broken, no matter what code behind will not be executed
Return can be seen as a place where a function ends.
}


Function Usage 2
$con can be an array
function f_2 ($arr, $con) {
First instance of a variable
$code = ' <ul> ';
foreach ($arr as $key => $value) {
The For loop inside is looping out the con content
foreach ($con as $value 2) {
. = add more continuous definition variables later
If the value of the first-tier data is recycled, the same value as the second-level conditional loop is added to the variable
Multiple for loops to filter data also known as recursion
if ($value = = $value 2) {
$code. = ' <li> '. $value. ' </li> ';
}
}
}
$code. = ' </ul> ';
Returns the variable after the loop gets the result. So, this function is just a string
return $code;
}

Function Usage 3
What's the difference between Echo and return in a function, see the execution results.
function F_3 ($arr, $con) {
First instance of a variable
Echo ' <ul> ';
foreach ($arr as $key => $value) {
The For loop inside is looping out the con content
foreach ($con as $value 2) {
. = add more continuous definition variables later
If the value of the first-tier data is recycled, the same value as the second-level conditional loop is added to the variable
Multiple for loops to filter data also known as recursion
if ($value = = $value 2) {
Echo ' <li> '. $value. ' </li> ';
}
}
}
Echo ' </ul> ';
}
?>

F_1 Output start<br/>
<?php
Because F_1 is an array, we can print it out
Print_r (F_1 ($array, ' banana '));
?>
<br/>f_1 Output End
F_2 Output start<br/>
<?php
F_2 is a variable
$con = Array (' Apple ', ' father ');
Echo f_2 ($array, $con);
?>
<br/>f_2 Output End
F_2 Output start<br/>
<?php
F_3 is already in the function echo, so do not echo when the function executes
$con = Array (' Apple ', ' father ');
F_3 ($array, $con);
?>
<br/>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.