PHP Expert Programmer

Just another WordPress.com weblog

Posts Tagged ‘Returning Array’

Returning an array of variables from PHP function

Posted by sarapeter on July 29, 2008

From a function we can get back a set of variables by using an array. A function returns any variable to the main script by using return statement. Here we will try to return a set of variables by using an array. Our main script will receive the array and we will use while each statement to display all elements of an array.

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.

<?Php

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>”;
}

?>

Posted in Php Codes | Tagged: , , , , | Leave a Comment »