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?

4
  • 3
    I guess that you can't return multiple variables Commented Feb 3, 2015 at 14:50
  • point 1: you miss to return anything if condition is true, point 2: you cant return 2 variables if you need both, return an array Commented Feb 3, 2015 at 14:51
  • 3
    The return is outside the if(), it's just bad formatting. Commented Feb 3, 2015 at 14:51
  • Possible duplicate of Multiple returns from function Commented May 29, 2016 at 4:04

5 Answers 5

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, should the list line in the class or the when I call the class. Thank you.
@ZYZ123ABC242526 Outside the class, right where you're calling the method.
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);

Comments

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];
}

1 Comment

Or yield but that gets a bit more complicated ;)
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

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.