We will change a script a bit and try to pass ( as input ) a string to the function. This string we will break by using split command and create an array. This array we will return to main script for displaying.
Here is the code ..
function test($my_string){
// creating an array by split command
$my_array=split(" ",$my_string);
return $my_array; // returning the array
}
// sending a string to function as input //
$collect_array=test("Hello welcome to plus2net");
// displaying the elements of the collected array
while (list ($key, $val) = each ($collect_array)) {
echo "$key -> $val <br>";
}
Passing an array as input to PHP function
Above we have discussed how to return an array from a function. Now we will discuss how to input or pass an array to a function.
We will break an string and create an array. This created array we will pass as input to the function. To test the array inside the function we will display it by using while each command.
Here is the code.
function test($my_array){
while (list ($key, $val) = each ($my_array)) {
echo "$key -> $val <br>";
}
}
$collect_array=split(" ","Hello welcome to plus2net");
test($collect_array);