0
<?php

function doSomething( &$arg ){
    $return = $arg;
    $arg += 1;
    return $return;
}

$a = 3;
$b = doSomething( $a );

echo $a.'  ';
echo $b;
?>

I know the answer a=4 and b=3 I understand how b= 3 but how come value of the a increased

brombeer
  • 8,716
  • 5
  • 21
  • 27
xmr6204
  • 47
  • 3

1 Answers1

3

The & operator tells PHP not to copy the variable when passing it to the function. Instead, a reference to the variable is passed into the function, thus the function modifies the original variable instead of a copy.

Therefore $arg += 1; will increase $a

Skip
  • 345
  • 3
  • 13