crmepham
crmepham

Reputation: 4740

Not able to retrieve multiple variables from a function

Hi I am trying to use the list() function to retrieve two variables from within a function. The function returns the to variables within an array.

function thumb_dimensions($case, $image_width, $image_height){
    switch($case){
        case 1:
            $thumb_width    =   100;
            $thumb_height   =   100;
        break;
        case 2:
            $thumb_height   =   100;
            $ratio          =   $thumb_height / $image_height;
            $thumb_width    =   round( $image_width * $ratio );
        break;
        case 3:
            $thumb_width    =   100;
            $ratio          =   $thumb_width / $image_width;
            $thumb_height   =   round($image_height * $ratio);
        break;


        return array($thumb_width, $thumb_height);
    }


}

$case = 3;
list($thumb_width, $thumb_height) = thumb_dimensions($case, $image_width, $image_height);

Upvotes: 0

Views: 72

Answers (1)

Marcelo Diniz
Marcelo Diniz

Reputation: 2499

The return statement is inside the switch but after the break, so it doesn't run. Your function doesn't return anything and the list fails.

Move the return statement out of the switch and it should be fine.

Upvotes: 5

Related Questions