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