0

I have a method in my class that I want to return more than one value:

public function lockFields($lockFieldsDBs, $lockValueConstant) {
    if ($lockFieldsDBs == $lockValueConstant) {
        $lockFieldValue = 'readonly="readonly"';
        $lockImage      = 'image_url_goes_here';
    }
    else {
        $lockFieldValue = null;
        $lockImage = null;
    }

    return $lockField;
    return $lockImage;
}


...........


$lockFieldsDBs = 'OK';
$lockValueConstant = 'OK';

$formHandler->lockFields($lockFieldsDBs, $lockValue);

When trying to print out a value like this: echo $lockImage;, I am getting Undefined variable: lockImage.

Why isn't the value being returned?

silkfire
  • 24,585
  • 15
  • 82
  • 105

5 Answers5

4

You can return two or more values from a function with help of the list() language construct:

public function lockFields($lockFieldsDBs, $lockValueConstant) {
    if($lockFieldsDBs == $lockValueConstant){
        $lockFieldValue = 'readonly="readonly"';
        $lockImage = 'image_url_goes_here';
    }
    else {
        $lockFieldValue = null;
        $lockImage = null;
    }

    return [$lockField, $lockImage];
}



list($lockField, $lockImage) = $formHandler->lockFields($lockFieldsDBs, $lockValue);
silkfire
  • 24,585
  • 15
  • 82
  • 105
1

The problem is you are returning more than one variable form you function. You can't. you have to assign an array or a list to do this job, to do this use either of these codes before ending your function:

return ['lockField'=>$lockField, 'lockImage'=>$lockImage];

or

return array($lockField, $lockImage);
0

You can only return one variable in a PHP function. If you need to return two, you need to use an array (or an object).

public function lockFields($lockFieldsDBs,$lockValueConstant)
{
    if($lockFieldsDBs == $lockValueConstant){
        $lockFieldValue = 'readonly="readonly"';
        $lockImage = 'image_url_goes_here';
    } else {
        $lockFieldValue = null;
        $lockImage = null;
    }
    return ['lockField'=>$lockField, 'lockImage'=>$lockImage];
}
Jessica
  • 7,075
  • 28
  • 39
0

You cannot return two variables in a php function:

return $lockField;
return $lockImage;

You can put them in an array:

return array($lockField, $lockImage);

to use it:

$response = $formHandler->lockFields($lockFieldsDB,$lockValue)
echo $response[0]; // this is $lockField in your function 
echo $response[1]; // this is $lockImage in your function
Paul Lo
  • 6,032
  • 6
  • 31
  • 36
0

$lockImage is a variable inside of the method. Why do you think it will be available globally? To be clear:

public function lockFields($lockFieldsDBs,$lockValueConstant){
  $lockImage = "some value";
  return $lockImage;
}

$formHandler->lockFields($lockFieldsDB,$lockValue);
echo $lockImage; // variable $lockImage does not exists

won't work. It should be:

public function lockFields($lockFieldsDBs,$lockValueConstant){
  $lockImage = "some value";
  return $lockImage;
}

$lockImage = $formHandler->lockFields($lockFieldsDB,$lockValue);
echo $lockImage; // "some value"

The other answers point out that you need to return the value properly. But I think you also do not fetch the return correctly.

agoldev
  • 2,078
  • 3
  • 23
  • 38