0

I have lots of lines this kind of code. call('someCustomFunctionName', array($paramA, &paramB, $paramC));

I have updated my server php version and it wont work anymore.

Here is SIMPLIFIED code for testing purpose. Expected output: $b is true and $c is 2

function call($function, $param_arr = array())
{   
    # Lots of code here.
    return function_exists($function) ? call_user_func_array( $function, (array) $param_arr) : '';
}

function test_a($a, $b, $c)
{
    if($a['a'] == 1)
    {
        $b = true;
        $c++;
    }
}

$a = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4);
$b = false;
$c = 1;

call('test_a', array($a, &$b, &$c));

if($b)
{
    print '$b is true';
}
else{
    print '$b is false';
}
print '<br>';
print $c;
5
  • 2
    What error do you get in the server logs? Or are you just not getting the expected output? Commented Jan 9, 2017 at 17:22
  • 1
    It's a "pass by reference" issue: you're passing by value, but still expecting those variables to be updated as though you were passing by reference. This may help Commented Jan 9, 2017 at 17:23
  • 2
    "it does not work" has never helped anyone anywhere at any time to get help with code. You need to be more specific in the details: what exact behavior and error do you get? Commented Jan 9, 2017 at 17:24
  • 1
    before server php upgrade everything worked. No errors. Expected $b value should be true i get false. Expected $c value should be 2 i get 1 Commented Jan 9, 2017 at 17:48
  • error_reporting(E_ALL); ini_set('display_errors', '1'); Commented Jan 9, 2017 at 18:13

1 Answer 1

0

The problem is in your test_a() function you feed the array with two references $b and $c, the function however does not accept a reference value and creates a copy instead.

The solution is simple:

function test_a($a, &$b, &$c){
  if($a['a'] == 1){
    $b = true;
    $c++;
  }
}
Sign up to request clarification or add additional context in comments.

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.