the difference is by value is not working the variable outside the function and the global function is working outside the function
by value

function increment($value,$amount = 1){
    $value = $value + $amount;
}
$value = 10;
increment($value);
echo $value;

reference
& is doing like a global variable is working outside the function

function increment(&$value,$amount = 1){
    $value = $value + $amount;
}
$a = 10;
echo $a.'<br/>';
increment($a);
echo $a.'<br/>';

    Any variable within a function is only in scope (exists) within that function. You can get around that by either declaring it to be global -- which has a lot of potential to be a problem, so should be an absolute last resort -- or by passing a variable by reference. I consider the latter to be more viable than declaring things global, but still a last resort when, for some reason, doing the usual thing of using return is problematic (very rare, for me). So in your example, just return the result of the operation, and if you want to have it alter the value of the variable you pass in to the function, then assign the returned value to it, e.g.:

    function increment($value, $amount = 1) {
        return $value + $amount;
    }
    
    $foo = 10; // doesn't matter what the variable name is
    $foo = increment($foo);
    echo $foo;
    
      Write a Reply...