Returning arrays from functions
In PHP you can return one and only one value from your user functions, but you are able to make that single value an array, thereby allowing you to return many values.
This following code shows how easy it is:
<?php
function dofoo() {
$array["a"] = "Foo";
$array["b"] = "Bar";
$array["c"] = "Baz";
return $array;
}
$foo = dofoo();
?>
Without returning an array, the only other way to pass data back to the calling script is by accepting parameters by reference and changing them inside the function. Passing arrays by reference like this is generally preferred as it is less of a hack, and also frees up your return value for some a true/false to check whether the function was successful.
Next chapter: Array-specific functions >>
Previous chapter: The array operator
Jump to:
Home: Table of Contents



Copyright 2012 Future Publishing Limited (company
registered number 2008885), a company registered
in England and Wales whose registered office is at
Beauford Court, 30 Monmouth Street, Bath, BA1 2BW, UK