How does passing variables by reference in PHP differ from passing by value?

When passing variables by reference in PHP, changes made to the variable within a function will affect the original variable outside of the function. This means that the function can directly modify the original variable's value. On the other hand, when passing variables by value in PHP, a copy of the variable is passed to the function, so any changes made within the function will not affect the original variable outside of the function.

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

$value = 5;
increment($value);
echo $value; // Output: 6

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

$value = 5;
increment($value);
echo $value; // Output: 5