Created
January 15, 2014 22:58
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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