Skip to content

Instantly share code, notes, and snippets.

@Rican7
Created January 15, 2014 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rican7/8446462 to your computer and use it in GitHub Desktop.
Save Rican7/8446462 to your computer and use it in GitHub Desktop.
In PHP there are multiple ways to get multiple return values in a "tuple-like" style. This GIST explores a few ways of doing so.
<?php
function return_me_data_pls() {
$params_to_return = array(
'count' => range(1,10),
'sum' => array_sum(range(1,10))
);
return $params_to_return;
}
// Manually
$returned = return_me_data_pls();
$count = $returned['count'];
$sum = $returned['sum'];
var_dump($count, $sum);
unset($returned, $count, $sum);
// Automatically
extract(return_me_data_pls());
var_dump($count, $sum);
unset($count, $sum);
// Listing
list($count, $sum) = array_values(return_me_data_pls());
var_dump($count, $sum);
unset($count, $sum);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment