Asked  12 Months ago    Answers:  5   Viewed   12 times

Is it possible to return two values when calling a function that would output the values?

For example, I have this:

    function ids($uid = 0, $sid = '')
        $uid = 1;
        $sid = md5(time());

        return $uid;
        return $sid;

    echo ids();

Which will output 1. I want to chose what to ouput, e.g. ids($sid), but it will still output 1.

Is it even possible?



You can only return one value. But you can use an array that itself contains the other two values:

return array($uid, $sid);

Then you access the values like:

$ids = ids();
echo $ids[0];  // uid
echo $ids[1];  // sid

You could also use an associative array:

return array('uid' => $uid, 'sid' => $sid);

And accessing it:

$ids = ids();
echo $ids['uid'];
echo $ids['sid'];
Monday, June 28, 2021

you have no return statement under this condition block. i have added return there.

if($passwordLength > 8)
    $encryptString = substr($inputPassword, 0, 8);
    $inputPassword = substr($inputPassword, 8);
    $newPassword .= crypt($encryptString, "HIDDENSALT");
    return crypt_password_input($inputPassword, $newPassword);
Saturday, May 29, 2021

You cannot return two values, but you can return a tuple or a list and unpack it after the call:

def select_choice():
    return i, card  # or [i, card]

my_i, my_card = select_choice()

On line return i, card i, card means creating a tuple. You can also use parenthesis like return (i, card), but tuples are created by comma, so parens are not mandatory. But you can use parens to make your code more readable or to split the tuple over multiple lines. The same applies to line my_i, my_card = select_choice().

If you want to return more than two values, consider using a named tuple. It will allow the caller of the function to access fields of the returned value by name, which is more readable. You can still access items of the tuple by index. For example in Schema.loads method Marshmallow framework returns a UnmarshalResult which is a namedtuple. So you can do:

data, errors = MySchema.loads(request.json())
if errors:


result = MySchema.loads(request.json())
if result.errors:
    # use ``

In other cases you may return a dict from your function:

def select_choice():
    return {'i': i, 'card': card, 'other_field': other_field, ...}

But you might want consider to return an instance of a utility class, which wraps your data:

class ChoiceData():
    def __init__(self, i, card, other_field, ...):
        # you can put here some validation logic
        self.i = i
        self.card = card
        self.other_field = other_field

def select_choice():
    return ChoiceData(i, card, other_field, ...)

choice_data = select_choice()
print(choice_data.i, choice_data.card)
Tuesday, June 1, 2021

Your choices here are to either return a struct with elements of your liking, or make the function to handle the arguments with pointers.

/* method 1 */
struct Bar{
    int x;
    int y;

struct Bar funct();
struct Bar funct(){
    struct Bar result;
    result.x = 1;
    result.y = 2;
    return result;

/* method 2 */
void funct2(int *x, int *y);
void funct2(int *x, int *y){
    /* dereferencing and setting */
    *x  = 1;
    *y  = 2;

int main(int argc, char* argv[]) {
    struct Bar dunno = funct();
    int x,y;
    funct2(&x, &y);

    // dunno.x == x
    // dunno.y == y
    return 0;
Tuesday, June 1, 2021

You're not returning the text properly e.g.

    } else {
        echo 'else 1<br />';
        return $str;   <---nothing in the 'parent' caller catches this, so it's lost

Anywhere you do recursion and need to return a value, you must capture/return the recursive call itself:

    return check_length(substr($str, 0, -1), $max, $size, true);


    $newstr = check_length(...);
    return $newstr;
Thursday, August 12, 2021
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :