What is the difference between passing variables by reference and by value in PHP?

When passing variables by value in PHP, a copy of the variable is passed to the function, so any changes made to the variable inside the function do not affect the original variable outside the function. On the other hand, when passing variables by reference, a reference to the original variable is passed to the function, so any changes made to the variable inside the function will also affect the original variable outside the function.

// Passing variables by value
function incrementValue($num) {
    $num++;
    return $num;
}

$value = 10;
$newValue = incrementValue($value);
echo $value; // Output: 10
echo $newValue; // Output: 11

// Passing variables by reference
function incrementValueByReference(&$num) {
    $num++;
}

$value = 10;
incrementValueByReference($value);
echo $value; // Output: 11